我似乎無法理解這裏發生了什麼。然而,當使用'this'關鍵字時,我的視圖不會在點擊按鈕上更新;如果我使用「範圍」,它會正確更新。我不確定自己的自定義指令中是否有錯誤。使用ControllerAs和自定義指令時,視圖不會更新
//My custom directive
app['directive']('myCustomDirective',function(){
return{
restrict: 'E',
templateUrl: 'templates/someTemplate.html',
controller: 'mainCtrl',
controllerAs: 'ctrl'
}
})
//My controller, will not update view on click
app['controller']('mainCtrl', ['$scope', function (scope) {
this['name'] ='Hello';
this['click'] = function(){
this['name'] = '';
}
})
//Portion of HTML from view.
<input type="text" ng-model="ctrl.name" required/>
<md-button class="md-primary" ng-click="ctrl.click()">Submit</md-button>
//However; if I use the scope (like below) it will update the view
app['controller']('mainCtrl', ['$scope', function (scope) {
scope['name'] ='';
this['click'] = function(){
scope['name'] = "You entered your name";
}
})
//Will update the update the view on button click.
<input type="text" ng-model="name" required/>
<md-button class="md-primary" ng-click="ctrl.click()">Submit</md-button>
'NG-模型= 「ctrl.name」 你''了controllerAs ctrl'在不工作的例子嗎? @ drivers34 – taguenizy
我不確定'controller:'mainCtrl', controllerAs:'ctrl''對於一個指令是可以的。您應該根據需要使用組件 – qchap
但是,使用「this」關鍵字時,它不起作用;當我綁定到「範圍」時,它沒有問題。 – drivers34