2017-10-08 102 views
0

似乎我的mdDialog彈出窗口不會按照應有的方式工作。mdDialog的退回承諾不起作用

function() { 
    $mdDialog.show({ 
     templateUrl: 'CameraPopup.html', 
     clickOutsideToClose: true  
    }).then(function (answer){ //do something 

           //with answer 
})} 

模板看起來是這樣的:

<md-dialog > 
<form> 
    <md-dialog-actions layout="column" > 
     <span flex></span> 
     <md-button ng-click="answer('camera')"> 
      Open Camera 
     </md-button> 
     <md-divider></md-divider> 
     <md-button ng-click="answer('gallery')"> 
      Open Gallery 
     </md-button> 
    </md-dialog-actions> 
</form></md-dialog> 

我知道問題出在它返回的承諾,但我不明白爲什麼這是行不通的。

回答

0

添加一個控制器。將該控制器傳入$mdDialog.show。將answer方法寫入控制器。

function() { 
     $mdDialog.show({ 
      controller: DialogController, 
      templateUrl: 'CameraPopup.html', 
      clickOutsideToClose: true  
      }).then(function (answer){ //do something with answer 
     }); 
    } 

控制器

function DialogController($scope, $mdDialog) { 
    $scope.answer = function(answer) { 
     $mdDialog.hide(answer); 
    }; 
    } 
});