使用来自app.run

时间:2016-02-17 09:32:28

标签: angularjs angularjs-scope broadcast angularjs-controller angularjs-rootscope

我有一个名为DemoProject的角度应用程序。

我有一个app.run控制器和一个子控制器

JS

var app = angular.module("DemoProject", ['ngRoute', 'ngAnimate', 'ngMessages', 'ngMaterial']);

app.run(function ($rootScope, $route, $location, $mdDialog) {
    $rootScope.validate = true;

    $rootScope.$broadcast('eventName', { myName: 'Bala' });

});

app.controller('ChildController', function ($scope, $location, $rootScope, $document, $window) {
    $scope.myName = '';
});

我无法使用广播更新子控制器范围。

1 个答案:

答案 0 :(得分:3)

app.controller('ChildController', function ($scope, $location, $rootScope, $document, $window) {
  $scope.myName = '';

  $rootScope.$on('eventName', function(event, args){
      console.log(args);
      $scope.myName = args.myName;
  });
});