简体   繁体   English

ng-show不更新

[英]ng-show not updating

I'm trying to get a broadcast to switch which view is currently being displayed, but when I change the controller's model, the ng-show doensn't change to reflect the new state. 我正在尝试广播以切换当前正在显示的视图,但是当我更改控制器的模型时,ng-show不会更改以反映新的状态。

I'm using $rootScope.$broadcast("registerFormShow", username, password) to tell the RegisterForm controller that I want it to be visible. 我正在使用$rootScope.$broadcast("registerFormShow", username, password)告诉RegisterForm控制器我希望它可见。 It has a variable register.active that I have in the ng-show, but it doesn't seem to apply the change. 我在ng-show中有一个变量register.active ,但似乎没有应用更改。 I've tried calling $scope.$apply() from within the $on callback, but angular throws an exception saying that $apply is already in progress . 我试过从$on回调中调用$scope.$apply() ,但是angular抛出一个异常,说$apply is already in progress Any ideas why this is happening? 任何想法为什么会这样?

Here's some of my code: 这是我的一些代码:

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: 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>

Here's a link to a demo demonstrating this problem: http://jsfiddle.net/7LLa7zot/1/ 这是演示此问题的演示的链接: http : //jsfiddle.net/7LLa7zot/1/

I would try 我会尝试

 this.active = false;
 var _this = this;
    $scope.$on("register", function(username, password){
        _this.active = true;
    }

Not sure if this is the problem but you never know which this is this in javascript. 不知道这是否是问题,但您永远不会在javascript中知道这是什么。

This fixed the fiddler so it is most likely your issue. 这样就解决了提琴手的问题,因此很可能是您遇到的问题。 So the issue incase you don't see it is you are using the incorrect "this" inside of your register. 因此,如果您没有看到问题,是因为您在寄存器中使用了错误的“ this”。

Parameters sent using the .$broadcast has to be sent as a object, not as seperate arguments. 使用。$ broadcast发送的参数必须作为对象而不是单独的参数发送。 Here is an example: 这是一个例子:

$rootScope.$broadcast("register", {
    'username': username,
    'password': password
});

http://jsfiddle.net/7LLa7zot/5/ http://jsfiddle.net/7LLa7zot/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM