when you use controllerAs, angular is adding a reference to the controller
onto the scope - named as you wish.

when you use a 'watch' or 'watchGroup' you are watching variables on the
scope. Therefore, you can watch items via the reference:

app.controller('MainCtrl', function MainCtrl($scope) {

    var self = this;

    this.vat1 = 123;
    this.var1 = 123;

    $scope.$watch('ctrl.var1', function() { // called when $scope.var1 is
changed }

    // or

    $scope.$watch(function() { return self.var2; // return the value using
a closure  } , function() { // called when $scope.var2 is changed }

    // using watchGroup

    $scope.$watchGroup( [
         'ctrl.var1',
         function() { return self.var2; }
    ], function() {
        // called when any of the above change
    });
});


Where you have the controller exported as 'ctrl':

<div  ng-controller="MainCtrl as ctrl"> ... </div>


note:
I actually like to explicitly export the controller to the scope itself
(not use controllerAs), im not sure the benefits of the official syntax,
especially if the controller relies on the reference being a certain value
(eg 'ctrl'):


app.controller('MainCtrl', function MainCtrl($scope) {

 // code here

   $scope.ctrl = this;
});





On 13 January 2015 at 17:22, Leonan Luppi <[email protected]> wrote:

> when you define ng-controller, just add ng-controll="MyController as
> controller"
>
> to use is similar:
>
> controller.my_method
> controller.my_object
>
> Em terça-feira, 13 de janeiro de 2015 22:03:18 UTC-2, Hoàng Ngọc Dũng
> escreveu:
>
>> Hi friends.
>> How can we use watch group with controllerAs systax. Thanks !
>> app.controller('MainCtrl', function MainCtrl($scope) {
>>   'use strict';
>>
>>   this.a = 1;
>>   this.b = 2;
>>
>>   $scope.$watchGroup([
>>     'a', ??
>>     'b'  ??
>>   ], function(newVal, oldVal) {
>>
>>   });
>> });
>>
>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Tony Polinelli

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to