我正在使用角度ng-show指令来检查用户是否是管理员用户,如果他们是,那么我希望“显示”某些html元素。
我首先在我的checkIfUserIsAdmin
中创建了名为mainController
的以下函数:
$scope.checkIfUserIsAdmin = function(){
var userPrivilegeID = sharedFactory.userDetails.userPrivilegeID;
if(userPrivilegeID === 2){
return true;
}else{
return false;
}
}
在我的HTML中我有以下内容:
<span ng-show="checkIfUserIsAdmin()"><i class="fa fa-check-circle"></i></span>
与ng-show配合良好,当userPrivilegeID更改值时,html按计划发生了变化。
但是我决定在工厂中定义这个功能,以便我可以将它传递给多个控制器。
但是现在当userPrivilegeID更改时,视图不会更新(因为它应该与ng-show一起使用)。抱歉,如果这是一个愚蠢的错误,但我一直试图弄清楚一段时间,并没有在网上找到任何东西。你能帮忙吗?
//create a factory so that we can pass these variables between different controllers.
myApp.factory('sharedFactory', function(){
//private variables
var userDetails = {
"userID" : null,
"userPrivilegeID" : 1,
"isLoggedIn" : false
};
var checkIfUserIsAdmin = function(){
var userPrivilegeID = userDetails.userPrivilegeID;
if(userPrivilegeID === 2){
return true;
}else{
return false;
}
};
//return public API so that we can access it in all controllers
return{
userDetails: userDetails,
checkIfUserIsAdmin: checkIfUserIsAdmin
};
});
myApp.controller("mainController", function($scope, sharedFactory){
$scope.checkIfUserIsAdmin = function(){
return sharedFactory.checkIfUserIsAdmin;
}
});
<body data-ng-controller="mainController">
<div id="container_wrapper">
<div class="container">
<span ng-show="checkIfUserIsAdmin()"><i class="fa fa-check-circle"></i></span>
<div ng-view>
<!--our individual views will be displayed here-->
</div>
</div>
</div>
</body>
编辑: 如上所示,userPrivilegeID初始化为1。但是,在我进行API调用后,它会被设置为2,但ng-show不会更新以显示html。 这是我的loginFactory,其中包含API调用
myApp.factory('loginFactory', function($http, $timeout, $q, sharedFactory){
//Methods which perform API calls
var checkLoginDetails = function(data){
var deferred = $q.defer();
$http({
method: 'POST',
url: 'http://localhost/API/auth?apiKey=0417883d',
data : JSON.stringify(data),
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
responseType:'json'
}).then(function successCallback(response){
if(response.hasOwnProperty('data') && response.data !== null){
console.log(JSON.stringify(response.data));
sharedFactory.userDetails = {
"userID" : response.data.userID,
"userPrivilegeID" : response.data.userPrivilegeID,
"isLoggedIn" : true
};
$timeout(function() {
deferred.resolve(sharedFactory.userDetails);
}, 100);
}else{
sharedFactory.buildErrorNotification(response);
}
},function errorCallback(response){
sharedFactory.buildErrorNotification(response);
});
//return the userDetails promise
return deferred.promise;
};
//return public API so that we can access it in all controllers
return{
checkLoginDetails: checkLoginDetails
};
});
然后在我的mainController中我有以下内容(调用checkLoginDetails函数):
$scope.loginWithFacebook = function(){
var data = {//...
};
loginFactory.checkLoginDetails(data).then(function(userDetails) {
//Since the checkLoginDetails method (in the loginFactory) is performing a http request we need to use a promise
//to store the userDetails (from the response) into our $scope.userDetails variable.
$scope.userDetails = userDetails;
});
}
答案 0 :(得分:1)
你打电话给你的服务部门时,你已经离开了parens。
myApp.controller("mainController", function($scope, sharedFactory){
$scope.checkIfUserIsAdmin = function(){
return sharedFactory.checkIfUserIsAdmin(); //<-- Needs to actually call the function.
}
});
将您的服务更改为以下内容:
//create a factory so that we can pass these variables between different controllers.
myApp.factory('sharedFactory', function(){
//private variables
var service = {
userDetails: {
"userID" : null,
"userPrivilegeID" : 1,
"isLoggedIn" : false
}
};
service.checkIfUserIsAdmin = function (){
var userPrivilegeID = service.userDetails.userPrivilegeID;
if(userPrivilegeID === 2){
return true;
}else{
return false;
}
};
//return public API so that we can access it in all controllers
return service;
});