简体   繁体   English

Angularjs 1.3.15-Promise返回后更新$ scope

[英]Angularjs 1.3.15 - Updating $scope after Promise Returned

I'm learning angularjs and trying to update nav bar with user name after login, but the user name does not appear. 我正在学习angularjs,并尝试在登录后使用用户名更新导航栏,但是用户名没有出现。 Here is the process I am following: 这是我正在遵循的过程:

  1. User logs in successfully. 用户登录成功。
  2. After 'then' in login promise (to make sure promise is returned), get user profile via factory which returns the user profile. 在登录承诺中“然后”之后(以确保返回了承诺),请通过工厂获取用户资料,该工厂将返回用户资料。
  3. After 'success' in getting user profile, save profile to $scope.user so that DOM is updated. 成功获取用户配置文件后,将配置文件保存到$ scope.user,以便更新DOM。

Here is HTML for Nav Bar (not sure if it matters but this is placed into index.html using ng-include on page load): 这是Nav Bar的HTML(不确定是否重要,但是在页面加载时使用ng-include将其放入index.html中):

<nav ng-controller="menuController">
    <div class="navbar-header">
        <a class="navbar-brand" href="index.html">
            Admin Site
        </a>
    </div>
    <ul >
        <li ><span>Welcome {{ user.firstName }}</span></li>
    </ul>
  </div>
</nav>

Here is the menuController js file: 这是menuController js文件:

    angular
    .module('myApp')
    .controller("menuController", ['$scope', '$auth', '$timeout', '$window', 'toaster', 'Account',
        function UserCtrl($scope, $auth, $timeout, $window, toaster, Account) {

            $scope.user = [];

            $scope.login = function () {
                $auth.login({email: $scope.email, password: $scope.password})
                    .then(function(response){
                        toaster.pop('success', "Logged In", "You have successfully logged into the system"); // This fires correctly
                        Account.getProfile()
                            .success(function(obj){
                                $scope.user = obj;
                                console.log('User: ' + 
$scope.user.firstName);  //This Works! But user.firstName in HTML is not updated
                            });
                    })
                    .catch(function (response) {
                        toaster.pop('error', "Login Failure", response.data.message);
                    })
            };   
        }
    ]);

HTML from index.html 来自index.html的HTML

<div id="wrapper">
        <div ng-include='"templates/navigation.html"'></div>

        <div ng-view></div>

</div>
<!-- /#wrapper -->

The problem is that the user.firstName in the navigation bar never updates with the user's first name. 问题是导航栏中的user.firstName永远不会更新为用户的名字。 Am I missing something? 我想念什么吗?

Thank you for any help! 感谢您的任何帮助!

What does Account.getProfile() return? Account.getProfile()返回什么? Is it angular promise or something outside angular framework? 它是有角度的承诺还是在有角度的框架之外的东西? Perhaps you should wrap the code inside $apply scope: 也许您应该将代码包装在$apply范围内:

Account.getProfile()
    .success(function(obj){
        $scope.$apply(function() {
            $scope.user = obj;
            console.log('User: ' + 
            $scope.user.firstName);  //This Works! But user.firstName in HTML is not updated
        });
    });

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

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