是否可以根据广播事件在控制器上设置属性?

时间:2014-09-18 08:16:38

标签: angularjs angularjs-scope

我有以下控制器;

function RecruiterSignUpCtrl(AuthService) {
    // Store Users Sign Up Details as an object to POST.
    this.user = {
        name          : '',
        company       : '',
        email_address : '',
        password      : ''
     };

     // userSignUp is called on submit on the recruiter signup form.
     this.userSignUp = function(user) {
        // All heavy lifting is abstracted into AuthService.
        // Pass the role up from the controller.
        AuthService.signUp(user, 'recruiters');
     }

     this.signUpError = false;

  }

我想在$ rootScope上广播某个事件时将signUpError更改为true。此事件从AuthService广播。

有人可以确认这是否可行?

1 个答案:

答案 0 :(得分:0)

请见http://jsbin.com/mijum/1/edit

var app = angular.module('app', []);

app.controller('firstCtrl', function($scope, $rootScope){


  $scope.brodcast = function(){

    $rootScope.$broadcast('loginFailed', true);

  };

});

app.controller('RecruiterSignUpCtrl',function($scope){


  $scope.signUpError = false;

  $scope.$on("loginFailed", function (e, loggedIn) {



    $scope.signUpError = loggedIn;

  });


});