簡體   English   中英

如何在角度UI模式控制器中定義函數

[英]How to define a function inside angular ui modal controller

我嘗試在默認情況下在angular ui模態控制器中定義一個函數,發現了兩個函數$ 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;
                }
              }
            });

當我在ng-click中調用函數deleteChoix時,這是我的html代碼,什么也沒有發生,並且該項目沒有從項目列表中刪除任何解決方案

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

這是一個限制Java繼承的范圍問題。
如果您不想遇到此問題,請始終使用以下中介對象:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM