简体   繁体   中英

How to define a function inside angular ui modal controller

I try to define a function inside angular ui modal controller by default a found the two function $scope.ok and the $scope.cancel and I want to add my function that remove an item form a list of items that a send to that controller This my angular ui modal controller code :

myapp.controller('ModalInstanceCtrl', function ($scope,$location,$uibModalInstance, items) {

      $scope.items = items;
      $scope.selected = {
        item: $scope.items[0]
      };

      $scope.ok = function () {
        $uibModalInstance.close($scope.selected.item);
        alert("redirection");
         $location.path('/questionnaire');
      };
      $scope.closeListeChoix = function () {
        $uibModalInstance.close($scope.selected.item);

      };
      $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
      };
      $scope.deleteChoix=function($index)
      {

          alert("supp")
          $scope.items.splice($index, 1);
      };
});

and here where I send the liste of items to modal controller

$scope.ListeChoixOneQuestion=[];
        $scope.openListeChoix = function ($index) {
            $scope.ListeChoixOneQuestion=$scope.questions[$index].choix ;
            console.log("*********** choix de la question **********")
             for(var i=0;i<$scope.ListeChoixOneQuestion.length;i++){
                 console.log("choix : "+$scope.ListeChoixOneQuestion[i].designation);
             }
            var modalInstance = $uibModal.open({
              animation: $scope.animationsEnabled,
              templateUrl: 'listeOfChoix.html',
              controller: 'ModalInstanceCtrl',
              resolve: {
                items: function () {
                  return $scope.ListeChoixOneQuestion;
                }
              }
            });

and this my html code when i call the function deleteChoix in my ng-click nothing happen and the item did not remove from the list of items any solution

 <div class="modal-body">
                  <div class="row">
                       <div class="table-responsive">
                            <table id="Table2" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>Designation</th>
                                        <th>Image</th>
                                        <th>Aller à la question</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="choix in items track by $index">
                                        <td>{{choix.designation}}</td>
                                        <td>{{choix.imageUrl}}</td>
                                        <td>{{choix.gotoQuestion}}</td>
                                        <td class="text-center" style="width: 50px;">
                                            <span class="btn btn-danger btn-xs fa fa-remove" style="cursor: pointer;" ng-click="deleteChoix($index);"></span>
                                        </td>
                                        <tr>
                                </tbody>
                            </table>
                        </div>
                   </div>
        </div>

As said in comment the short solution is

$parent.deleteChoix($index);

It is a scope problem du to limit of inheritancy in Javascript.
If you don't want to have this problem, always use an inermediary object like :

 $scope.context = {};// NEVER forget to initialize it in your controller or it won't work even if you don't put anything in at the start.
 $scope.context.deleteChoix = [...]; 

So you won't need to wonder if you should use $parent or even $parent.$parent .

Check http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/ for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM