[AngularJS] $scope & controllerAs
When you use $scope, you can no longer user controllerAs syntax to get the value from the scope.
controllerAs:
angular.module('SomeController')
.directive('nwCard', function(){
return{
restrict: "E",
templateUrl: "templates/directives/nwCard.html",
controller: function(){
this.header = "Note title";
},
controllerAs: "card"
}
})
<div class="card"> <h2 class="h3">{{card.header}}</h2> </div>
$scope:
angular.module('SomeController')
.directive('nwCard', function(){
return{
restrict: "E",
templateUrl: "templates/directives/nwCard.html",
controller: function($scope){
$scope.header = "Note title";
}
}
})
<div class="card"> <h2 class="h3">{{header}}</h2> </div>
When using the controllerAs syntax, the controller's context (this) attaches things to the current scope behind the scene.

浙公网安备 33010602011771号