2016-05-02 21 views
1

角度uiモーダルコントローラの内部で関数を定義しようとすると、デフォルトで$ scope.okと$ scope.cancelの2つの関数が見つかりました。角度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; 
       } 
       } 
      }); 

とこれをモーダルする項目のLISTEを送る:アイテムは、そのコントローラ へのセンドこの私の角度UIモーダルコントローラコード項目のリストを形成します私のHTMLコード私は関数の呼び出しを呼び出すとき私のNGクリック何でeChoixが起こると、アイテムがアイテム 任意の解決策のリストから削除しませんでした

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

したがって、モーダルの有効範囲で関数を定義しても機能しません。 $ parent.deleteChoix($ index);を試すことができますか。それは単なる小切手ですが、それが機能するかどうかを説明します。 – Walfrat

+0

Thx Manそれは仕事です!私はなぜ理解していないのですか? –

答えて

2

としては短いソリューションは、それはスコープの問題デュがある

$parent.deleteChoix($index); 

あるコメントで言いましたJavascriptの継承を制限するだから、あなたが$parentあるいは$parent.$parentを使用するかどうか疑問に思う必要はありません

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

:あなたはこの問題を持ってしたくない場合は
は、いつものようにinermediaryオブジェクトを使用します。

詳細については、http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/を確認してください。

関連する問題