Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Angular 1.3 - How to use $scope.$on inside controller because $scope is no longer work #284

Closed
lovedota opened this issue Apr 25, 2015 · 2 comments

Comments

@lovedota
Copy link

Hi guys.
Sorry I am new to angular-new-router. How can I upgrade from angular-route to angular-router
I changed using $scope to this, but some event subscribing like ``$scope.$on` How can I replace them. Thanks !

@jameswyse
Copy link

I'm using the activate method (add it to your controller's prototype) to access $scope at the moment. Not sure if it's ideal, or even intended, but it works!

From memory, something like this should work:

class YourController {
  constructor (UserModel) {
    this.UserModel = UserModel;
  }

  activate ($scope) {
    $scope.$on('userUpdated', (e, user) => {
      $scope.user = new this.UserModel(user);
    });
  }
}

edit: I'm using 1.4 so YMMV.

@btford
Copy link
Contributor

btford commented Apr 26, 2015

This has since been fixed in master; you can now ask for $scope in controller constructors.

@btford btford closed this as completed Apr 26, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants