4

Since we cannot inject $scope inside controllers in Angular 1.4+, how can we watch expressions the way we used to do with $scope.$watch?

An attempt to inject $scope can be seen here ("Could not instantiate controller" error), and tutorials tells us that:

Angular wont be able to instantiate the controller if we pass $scope in it. It defines its observable properties on this. (source)

sworded
  • 2,419
  • 4
  • 31
  • 48
zVictor
  • 3,610
  • 3
  • 41
  • 56

3 Answers3

2

I have just found out that there is a bug on ngNewRouter causing this problem. It has been fixed, but haven't been released yet.

To workaround it, try this:

$ git clone git@github.com:angular/router.git
$ cd router
$ npm install
$ gulp angularify

after that, copy the dist files to your project

zVictor
  • 3,610
  • 3
  • 41
  • 56
2

I ran into this and needed an npm package; no good having everyone on the team build from source for the time being. We released an interim package on npm that fixes this issue: npm install loomio-angular-router@0.5.7

Note: you may need to rename ng-viewport to ng-outlet to get the interim package working.

zVictor
  • 3,610
  • 3
  • 41
  • 56
gdpelican
  • 568
  • 4
  • 12
  • There is also an issue with $componentLoaderProvider, how can I resolve it? Unknown provider: $componentLoaderProvider – Dor Cohen Feb 25 '16 at 09:38
2

you have $watch available in .activate. Check bellow (copy paste from my app).

function accountHistoryController (authService, accountFactory, CONSTANTS, $q, $routeParams) {

}
accountHistoryController.prototype.canActivate = function() {
    return !!this.authService.isAuthenticated();
}
accountHistoryController.prototype.activate = function($scope) {
    console.log ($scope);
    $scope.$watch('_this.currentPage', function(newPage){
    if (newPage == undefined) return;

});

}
Alexandru R
  • 8,560
  • 16
  • 64
  • 98