我試圖讓廣播切換當前顯示的視圖,但是當我更改控制器的模型時,ng顯示doensn不會更改以反映新狀態。ng顯示不更新
我使用$rootScope.$broadcast("registerFormShow", username, password)
告訴RegisterForm控制器我希望它可見。它在ng顯示中有一個變量register.active
,但似乎並不適用這個變化。我試過在$on
回調內呼叫$scope.$apply()
,但是角度引發了一個異常,說$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/
燁,該固定它。我忘記了,在回調函數中,這指向了一個不同的對象。謝謝! – CoderOfHonor