2016-08-02 67 views
0

傳入AngularJS模態數據我有一雙控制器,看起來像這樣:使用控制器和VM

(function() { 
    'use strict'; 

    angular 
     .module('room') 
     .controller('RoomGetCtrl', Room) 
     .controller('TestCtrl', Test) 

    Room.$inject = [...'$uibModal'...]; 
    Test.$inject = [...'$uibModalInstance'...]; 

    function Room(..., $scope, $uibModal) { 
     var vm = this; 

     ... 

     vm.open = function (size) { 

      vm.modalInstance = $uibModal.open({ 
       templateUrl: 'modal.html', 
       controller: 'TestCtrl as vm', 
      }); 
     }; 
    } 

    function Test(???){ 
     this.modalText = 'Modal Text' 
     this.modalCancel = function() { 
      ???.dismiss(); 
     } 
    } 
})(); 

視圖看起來像這樣:

<script type="text/ng-template" id="modal.html"> 
    <div class="modal-header"> 
     <h3 class="modal-title">Modal window</h3> 
    </div> 
    <div class="modal-body"> 
     <pre>{{ vm.modalText }}</pre> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-default" ng-click="vm.modalCancel()">Cancel</button> 
    </div> 
</script> 

<button type="button" class="btn btn-default" ng-click="vm.open()">Open me!</button> 
... 

上述工作,不同之處在於我無法弄清楚???上面Test()的內容。我嘗試了各種各樣的東西,無論何時單擊取消按鈕,控制檯都會按照「x.dismiss不是函數」的方式記錄錯誤,其中「x」是我嘗試使用的任何東西。

任何幫助?

回答

0

沒關係。我馬上自己解決了這個問題(我發誓,發佈這個問題讓我覺得更好)。無論如何,在這裏:

(function() { 
    'use strict'; 

    angular 
     .module('room') 
     .controller('RoomGetCtrl', Room) 
     .controller('TestCtrl', Test) 

    Room.$inject = [...,'$uibModal']; 
    Test.$inject = [$uibModalInstance]; 

    function Room(..., $uibModal) { 
     /*jshint validthis: true */ 
     var vm = this; 

     ... 

     vm.open = function() { 

      vm.modalInstance = $uibModal.open({ 
       templateUrl: 'modal.html', 
       controller: 'TestCtrl as vm', 
      }); 
     }; 
    } 

    function Test($uibModalInstance){ 
     this.modalText = 'Modal Text' 
     this.modalCancel = function() { 
      $uibModalInstance.dismiss(); 
     } 
    } 
})(); 

真的很簡單。 $uibModalInstance.dismiss();。這是正確的,在該documentation

嘆息