![](/img/trans.png)
[英]AngularJS scope variables not updating in jQuery promise/deferred
[英]Angularjs 1.3.15 - Updating $scope after Promise Returned
我正在学习angularjs,并尝试在登录后使用用户名更新导航栏,但是用户名没有出现。 这是我正在遵循的过程:
这是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>
这是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);
})
};
}
]);
来自index.html的HTML
<div id="wrapper">
<div ng-include='"templates/navigation.html"'></div>
<div ng-view></div>
</div>
<!-- /#wrapper -->
问题是导航栏中的user.firstName永远不会更新为用户的名字。 我想念什么吗?
感谢您的任何帮助!
Account.getProfile()
返回什么? 它是有角度的承诺还是在有角度的框架之外的东西? 也许您应该将代码包装在$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.