我是AngularJS社区的新手,希望有人可以帮助我解决以下问题。
我已经基于一个不完整的教程创建了一个轻量级的CMS系统,并亲自填写了一些内容,但是当$ scope更改时,我无法更新HTML部分。
HTML部分(admin-login.html)
<div ng-if="loggedInUser">
Welcome {{loggedInUser}} | <a href="admin/pages">My Admin</a> | <a href="admin/logout">Logout</a>
</div>
我的指令(directives.js)
directive('adminLogin', [
function() {
return {
controller: function($scope, $cookies) {
var user = $cookies.get('loggedInUser', {path: "/"});
$scope.loggedInUser = user;
},
templateUrl: 'partials/directives/admin-login.html'
};
}
])
我的控制器(controllers.js)
controller('AdminLoginCtrl', ['$scope', '$location', '$cookies', 'AuthService','$log','flashMessageService',
function($scope, $location, $cookies, AuthService, $log, flashMessageService) {
$scope.credentials = {
username: '',
password: ''
};
$scope.login = function(credentials) {
AuthService.login(credentials).then(
function(res, err) {
$cookies.put('loggedInUser', res.data);
$location.path('/admin/pages');
},
function(err) {
flashMessageService.setMessage(err.data);
$log.log(err);
});
};
}
])
范围更新,但我必须刷新页面以显示或隐藏admin-login.html。
非常感谢您的帮助,在此先感谢您。
$scope.loggedInUser
一旦更新cookie,将不会更新,您应该注意指令中cookie的更改并$scope
手动更新。
directive('adminLogin', [
function() {
return {
controller: function($scope, $cookies) {
$scope.$watch(function () {
return $cookies.get('loggedInUser', {path: "/"});
}, function (value) {
$scope.loggedInUser = value;
});
},
templateUrl: 'partials/directives/admin-login.html'
};
}
])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句