javascript - 如何在angular ui modal controller中定义一个函数

标签 javascript angularjs angular-ui

我尝试在默认情况下在 Angular UI 模式 Controller 中定义一个函数,找到两个函数 $scope.ok 和 $scope.cancel 我想添加我的函数,从发送的项目列表中删除一个项目到那个 Controller 这是我的 Angular ui 模式 Controller 代码:

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

在这里我将项目列表发送给模态 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;
                }
              }
            });

这是我的 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/获取更多信息。

关于javascript - 如何在angular ui modal controller中定义一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982259/

相关文章:

javascript - 如何将用户 uid 从登录页面传递到 Web 应用程序中的不同页面

javascript - 将代码从 PHP 移动到 .js 文件,代码不起作用

javascript - AngularJS - 为值(value)增值

javascript - 如何在 Angular ui-router 状态下添加 guid 正则表达式

javascript - 使用转义键关闭模态的 Angular 指令

javascript - 如何选择数组中的最后一个东西?

javascript - 如何将一个网页嵌入另一个网页并隔离 CSS

javascript - 类型错误 : Cannot set property '' of undefined

javascript - 如何在javascript中获取不同地点的时区偏移量

angularjs - 如何重新加载当前状态?