AngularJS ng-show 无法使用我在工厂中定义的函数

AngularJS ng-show not working with my function defined in a factory

我正在使用 angular ng-show 指令来检查用户是否是管理员用户,如果是,那么我希望某些 html 元素是 "shown"。

我首先在 mainController 中创建了以下名为 checkIfUserIsAdmin 的函数:

 $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 一起运行良好,并且 html 在 userPrivilegeID 更改值时按计划更改。

但是我决定我想在工厂中定义这个函数,这样我就可以将它传递给多个控制器。

但是现在,当 userPrivilegeID 更改时,视图不会更新(使用 ng-show 时应该如此)。如果这是一个愚蠢的错误,我深表歉意,但我已经尝试解决了一段时间,但没有在网上找到任何东西。你能帮忙吗?

sharedFactory.js

//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
    };
});

mainController.js

 myApp.controller("mainController", function($scope, sharedFactory){
        $scope.checkIfUserIsAdmin = function(){
            return sharedFactory.checkIfUserIsAdmin; 
        }  
    });

index.html 文件(与这个问题最相关的部分)

 <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;
    });

}  

您在调用您的服务函数时遗漏了括号。

 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;
});