Working with $scope.$emit and $scope.$on

前端 未结 12 1239
花落未央
花落未央 2020-11-21 15:22

How can I send my $scope object from one controller to another using .$emit and .$on methods?

function firstCtrl($scop         


        
12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 15:31

    Scope(s) can be used to propagate, dispatch event to the scope children or parent.

    $emit - propagates the event to parent. $broadcast - propagates the event to children. $on - method to listen the events, propagated by $emit and $broadcast.

    example index.html:

    Root(Parent) scope count: {{count}}

    Childrent scope count: {{count}}

    example app.js:

    angular.module('appExample', [])
    .controller('EventCtrl', ['$scope', function($scope) {
      $scope.count = 0;
      $scope.$on('MyEvent', function() {
        $scope.count++;
      });
    }]);
    

    Here u can test code: http://jsfiddle.net/zp6v0rut/41/

提交回复
热议问题