Angular.js “Controller as …” + $scope.$on

前端 未结 3 948
滥情空心
滥情空心 2021-01-17 10:42

If i\'d like to use the \"Controller as ...\" syntax in Angular, how should I approach things like $scope.$on(...) that i need to put inside the controller?

I get a

3条回答
  •  孤城傲影
    2021-01-17 10:55

    Inject $scope and your controller is accessible by whatever you named it

    EG:

    $stateProvider
      .state('my-state', {
        ...
        controller: 'MyCtrl',
        controllerAs: 'ctrl',
        ...
      });
    
    
    
    .controller('MyCtrl', function($scope) {
      var $this = this;
      $scope.$on('ctrl.data', function(new, old) {
        // whatevs
      });
      $timeout(function() {
        $this.data = 'changed';
      }, 1000);
    });
    

提交回复
热议问题