I am trying to put some data in the scope which my directive create. Here is my jsFiddle.
the following code works well
.directive('directive1', function () {
return: {
scope: true,
controller: function ($scope) {
$scope.name = 'world';
}
}
})
<div directive1>
<p>{{ name }}</p>
</div>
but these code do not work
.directive('directive2', function () {
return: {
scope: true,
controller: function () {
this.name = 'world';
},
controllerAs: 'testCtrl'
}
})
<div directive2>
<p>{{ testCtrl.name }}</p>
</div>
Is there anything wrong in my code? or did I misunderstand something about controllerAs
?
ControllerAs support for directives was added in 1.2.0, so you'll have to use most recent version, instead of 1.0.2
from linked fiddle. This way it works like you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With