Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs controller in directive using controllerAs can not work?

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?

like image 830
user2331095 Avatar asked Sep 21 '13 10:09

user2331095


1 Answers

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.

like image 194
Klaster_1 Avatar answered Nov 15 '22 17:11

Klaster_1