AngularJs中的用户页面上未显示错误消息

时间:2016-10-11 11:50:09

标签: javascript angularjs

我在抛出异常后向用户显示消息时遇到问题。 我正在做的是尝试以不引人注意的用户身份登录。在这种情况下服务器返回401并消息“错误的用户名和密码组合”

在我的控制器中,我正在执行此操作:

class LoginOnlineUserController {
    constructor($state, AuthenticationService, LoginOnlineUserService) {
        this.$state = $state;
        this.AuthenticationService = AuthenticationService;
        this.LoginOnlineUserService = LoginOnlineUserService;

        this.username = null;
        this.password = null;
    }

    login() {
        let self = this;

        this.LoginOnlineUserService.login({
            username: this.username,
            password: this.password
        }).then(function(data) {
            self.AuthenticationService.login(data.jwtToken);

            if(self.AuthenticationService.isAuthenticated()) {
                self.$state.go("hrTesting");
            }
        }).catch(function(msg) {
            //alert(msg.data);
            self.error = msg.data;
        });
    }
}

export default LoginOnlineUserController;

如果函数isAuthenticated()执行得很好,并且令牌响应,则将该令牌提供给用户。 如果发生异常,则进入catch块并在客户端页面上写入消息。

我正在使用ng-if:

<div ng-if="self.error" class="alert alert-danger">{{self.error}}</div>

但是消息永远不会显示出来。当我尝试从异常中提醒消息时,我可以清楚地看到它,但用户页面上不存在错误消息。 有没有人在这里看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

您确定,您正在绑定您的控制器,如ng-controller="MyCtrl as self"吗?这对于在模板内部使用至关重要。