我正在尝试广播来切换当前正在显示的视图,但是当我更改控制器的模型时,ng-show不会更改以反映新状态。
我正在使用$rootScope.$broadcast("registerFormShow", username, password)
告诉RegisterForm控制器我希望它可见。它有一个变量register.active
,我在ng-show中有,但它似乎没有应用更改。我尝试在$scope.$apply()
回调中调用$on
,但是角度引发了$apply is already in progress
的异常。任何想法为什么会发生这种情况?
这是我的一些代码:
app.controller("LoginController", ["$http", "$rootScope", function($http, $rootScope){
this.openRegister = function(username, password){
$rootScope.$apply(function(){
$rootScope.$broadcast("register", username, password);
});
this.loggedIn = true;
console.log("register even sent");
}
}]);
app.controller("RegisterController", ["$http", "$scope", function($http, $scope){
this.active = false;
$scope.$on("register", function(username, password){
this.active = true;
}
}]);
HTML:
<div ng-controller="RegisterController as register" ng-show="register.active" class="cover">
<div class="form register-form" ng-class="{indeterminate: register.indeterminate}">
<input type="text" name="username" required="" ng-model="username" />
<input type="password" required="" ng-model="password" />
<button ng-click="register.register(username, password)">Register</button>
</div>
</div>
以下是演示此问题的演示的链接:http://jsfiddle.net/7LLa7zot/1/
答案 0 :(得分:1)
使用。$ broadcast发送的参数必须作为对象发送,而不是作为单独的参数发送。这是一个例子:
$rootScope.$broadcast("register", {
'username': username,
'password': password
});