2016-05-19 3 views
0

Noob警告!ui-bootstrap:指令コントローラ内からモーダルインスタンスにアクセス

ここで問題は、私が持っている正確に一つです。しかし、私は正しいとして選ばれた答えを理解していないangularjs - Accessing ui-bootstrap modal dismiss and close function from another controller

!私は言葉を理解するが、コントローラを共有する方法を理解していない。

私はモーダルを開くアプリケーションコントローラを持っており、モーダルのテンプレート内にはディレクティブがあります。私は、ディレクティブのコントローラ内からモーダルインスタンスを操作できるようにしたいと考えています。ここで

は私のマークアップです:モーダルの身体で

 <script type="text/ng-template" id="settingsModal"> 
      <div class="modal-header"> 
       <h3 class="modal-title">Confirm update!</h3> 
      </div> 
      <div class="modal-body"> 
       <calendar-settings cid="calendarId"/> 
      </div> 
      <div class="modal-footer"> 
       <!-- I want these buttons inside the directive instead --> 
       <button class="btn btn-primary" ng-click="ok()">OK</button> 
       <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 
      </div> 
     </script> 

、私はcalendarSettingsディレクティブを呼び出しています、と私はモーダルインスタンスの.dismissと.closeメソッドを使用できるようにしたいと思います私の指示の中から。

  var modalInstance = $modal.open({ 
       templateUrl: 'settingsModal', 
       controller: 'ModalInstanceController', 
       resolve: { 
        item: function() { 
         return $scope.sEntry; 
        }, 
        cid: function() { 
         return id; 
        } 
       }, 
       reject: { 
        item: function() { 
         return $scope.sEntry; 
        }, 
        cid: function() { return null; } 
       } 
      }); 

そして、ここでModalInstanceControllerです:ここで

は、openメソッドは次のようになります。

calendarsApp.controller('ModalInstanceController', function($scope, $modalInstance, item, cid){ 
     $scope.item = item; 
     $scope.calendarId = cid; 
     console.log(item); 

     $scope.ok = function() { 
      $modalInstance.close($scope.item); 
     }; 

     $scope.cancel = function() { 
      $modalInstance.dismiss('cancel'); 
     }; 
    }); 

私は私がやりたいことは簡単だと思いますが、私は公開するかどうかはわかりませんモーダル・インスタンス・コントローラーをディレクティブに追加するか、別のアプローチがあるかどうかを考えなければなりません。

注:この指示文のコードはここに貼り付けませんでした。しかし私は、私のディレクティブのコントローラーの中からモーダルインスタンスのメソッドを閉じて却下しようとしています。

ありがとうございました!

答えて

0
So 

    var modalInstance = $modal.open({ 
        templateUrl: 'settingsModal', 
        controller: 'ModalInstanceController', 
        resolve: { 
         item: function() { 
          return $scope.sEntry; 
         }, 
         cid: function() { 
          return id; 
         } 
        }, 
        reject: { 
         item: function() { 
          return $scope.sEntry; 
         }, 
         cid: function() { return null; } 
        } 

       //This comes from ModalInstanceController result is     
        //equivalent to whatever u send back 
       }).result.then(function(result){ 
     console.log(result); //Press f12 on chrome and go to console. 
    }); 



calendarsApp.controller('ModalInstanceController', function($scope, $modalInstance, item, cid){ 
     $scope.item = item; 
     $scope.calendarId = cid; 
     console.log(item); 

     $scope.ok = function() { 
      //You are sending back $scope.item but if you want everything 

//send back the $scope which is equivalent to sending back //ModalInstanceController 
     // $modalInstance.close($scope.item); 
      $modalInstance.close($scope); 
     }; 

     $scope.cancel = function() { 
      $modalInstance.dismiss('cancel'); 
     }; 
    }); 
+0

また、plain JSの代わりにtypecriptを使用します。これは多くの助けになります。 – hidden

+0

ありがとうございます!私はあなたが言ったことを理解していると思うが、私はまだディレクティブのコントローラ内からこれらのものにアクセスする方法を理解しているとは思わない。ディレクティブのコードを質問に入れませんでしたが、独自の分離スコープを持つcalendarSettingsというディレクティブがあり、そこからモーダルインスタンスにアクセスしようとしていて、ディレクティブのコントローラからcloseまたはdismissメソッドを呼び出そうとしています – MoDiggity

関連する問題