[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.

posted @ 2015-02-27 17:50  Zhentiw  阅读(255)  评论(0)    收藏  举报