我想將一些數據放在我的指令創建的範圍中。這是我的jsFiddle。使用控制器的指令中的angularjs控制器無法工作?
下面的代碼工作得很好
.directive('directive1', function() {
return: {
scope: true,
controller: function ($scope) {
$scope.name = 'world';
}
}
})
<div directive1>
<p>{{ name }}</p>
</div>
但這些代碼不工作
.directive('directive2', function() {
return: {
scope: true,
controller: function() {
this.name = 'world';
},
controllerAs: 'testCtrl'
}
})
<div directive2>
<p>{{ testCtrl.name }}</p>
</div>
這有什麼錯在我的代碼?還是我誤解了controllerAs
?
我不知道你是否可以這樣做。你定義了沒有'$ scope'的控制器。什麼是'controllerAs'? –
@MaximShoustin [angular1.2 docs](http://docs.angularjs.org/guide/directive),指令 – user2331095
的新功能知識:) –