如何在angular ui模态控制器中定义函数

How to define a function inside angular ui modal controller

本文关键字:控制器 定义 函数 模态 ui angular      更新时间:2023-09-26

我试图在angular ui模态控制器中定义一个函数,默认情况下a找到了两个函数$scope.ok和$scope.cancel,我想添加我的函数,从发送到该控制器的项目列表中删除一个项目这是我的角度ui模态控制器代码:

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);
      };
});

在这里,我将项目列表发送到模式控制器

$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;
                }
              }
            });

这是我的html代码,当我在ng-click中调用函数deleteChoix时,没有发生任何事情,项目也没有从项目列表中删除任何解决方案

 <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>

正如评论中所说,短期解决方案是

$parent.deleteChoix($index);

Javascript中继承性的限制是一个范围问题
如果你不想有这个问题,总是使用一个惰性对象,比如:

 $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 = [...]; 

因此,您不需要怀疑是否应该使用$parent甚至$parent.$parent

检查http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/了解更多信息。