Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS ng show不使用工厂中定义的my函数_Javascript_Angularjs_Angularjs Ng Show - Fatal编程技术网

Javascript AngularJS ng show不使用工厂中定义的my函数

Javascript AngularJS ng show不使用工厂中定义的my函数,javascript,angularjs,angularjs-ng-show,Javascript,Angularjs,Angularjs Ng Show,我使用angular ng show指令来检查用户是否是管理员用户,如果是,那么我希望“显示”某些html元素 我首先在mymainController中创建了名为checkIfUserIsAdmin的以下函数: $scope.checkIfUserIsAdmin = function(){ var userPrivilegeID = sharedFactory.userDetails.userPrivilegeID; if(userPrivilegeID =

我使用angular ng show指令来检查用户是否是管理员用户,如果是,那么我希望“显示”某些html元素

我首先在my
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>
mainController.js index.html文件(与此问题最相关的部分) 然后在我的主控制器中有以下内容(调用checkLoginDetails函数):


您在调用服务功能时忽略了paren

 myApp.controller("mainController", function($scope, sharedFactory){
        $scope.checkIfUserIsAdmin = function(){
            return sharedFactory.checkIfUserIsAdmin(); //<-- Needs to actually call the function.
        }  
    });

看起来您并没有真正调用工厂函数,只是返回工厂函数。您需要在控制器中的
checkIfUserIsAdmin
调用中添加括号。@Baldy oops。谢谢我现在这么做了。但它仍然没有像预期的那样工作。更多信息:如您所见,userPrivilegeID被初始化为1。然后我对API执行http请求,当我得到响应时,我将userPrivilege更新为2。然而,我希望html在这一点上会随着ng show的改变而改变,但事实并非如此。当我从http请求得到响应时,我正在使用承诺。这可能是问题的原因吗?谢谢你能分享API调用的代码吗?@JC Ford我刚刚编辑了我的问题,将API调用的代码包括在内。我看到了。为什么
$timeout
?谢谢。请参阅我上面对“Baldy”的评论,因为它仍然无法按预期工作。在API promise处理程序中,您正在设置
sharedFactory.userDetails
,但在
sharedFactory
服务中,
userDetails
是一个局部变量,而不是属性。该局部变量保留在
checkIfUserIsAdmin
函数的闭包中。函数从不将存储的值视为属性。哦,是的,我明白了。userDetails变量只有在我最后从sharedFactory返回时才变为全局变量。尽管我必须这样返回它,否则会导致错误:返回{service:service};
 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>
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
    };
});
$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;
});