执行主控制器后执行控制器

时间:2016-10-31 07:11:11

标签: angularjs

我的控制器var typeOf = function(value) { if (typeof value == "object") { return Array.isArray(value) ? "array" : "object"; } return typeof value; }; 有一个索引页index.html。我在indexController中的factory中设置了一些值($ scope.ipAddress),indexController中的html的其他控制器使用了该值。

加载页面时,ng-view应首先执行,在indexController中设置一个值,然后该值由factory内的其他页面控制器使用。< / p>

但我认为其他控制器在ng-view之前首先被执行,并且我为我设置的值未定义。

这是indexController indexController

factory

我使用工厂值的另一个控制器:

        angular.module('tollApp')
.controller('indexController', function($scope,$http,$window,userDetailsFactory){
    $scope.usernameFromServer={};
    $scope.getUserDetails = function(){
        $http({
            method:'GET',
            url:'http://192.168.1.80:4000/getUserDetails'
            // url:'http://websitename.com/getUserDetails'
        })
        .then(function(response){

            // console.log(JSON.stringify(response));
            userDetailsFactory.setUserDetailsInFactory(response.data);
        $scope.usernameFromFactory = userDetailsFactory.getUserDetailsFromFactory().usernameFromSession;
        $scope.theIP = userDetailsFactory.getUserDetailsFromFactory().ipAddress;
            // $scope.usernameFromServer = userDetailsFactory.getUserDetailsFromFactory().username;
            // console.log(JSON.stringify($scope.usernameFromFactory)+"usernameFromFactory");
        })
    }
    $scope.logout = function(request,response){
        $http({
            method:'GET',
            url:'/logout'
        })
        .then(function(response){
            console.log(JSON.stringify(response));
            if(response.data=="logout"){
                // $window.location.href="http://websitename.comlogin";
                $window.location.href="http://192.168.1.80:4000/login";

            }
        })
    }
    console.log("indexController");
}).factory('userDetailsFactory',function(){
    var user = {};
    return {
        setUserDetailsInFactory : function(val){
            user.useridFromSession = val[0].UserID;
            user.usernameFromSession = val[0].UserName;
            user.userroleFromSession = val[0].UserRole;
            user.clientidFromSession = val[0].ClientID;
             user.ipAddress = "http://192.168.1.80:4000/";
            // user.ipAddress = "http://websitename.com/";
            // console.log("in set "+user.clientidFromSession);
        },
        getUserDetailsFromFactory : function(){
            return user;
        }
    };
})

1 个答案:

答案 0 :(得分:1)

查看您的$scope.getUserDetails - 您正在使用$httpdocs)服务对后端进行asycronous AJAX调用。

您在返回对象的.then()方法中提供的回调函数可能在将来某个时间执行,甚至可能不执行 - 取决于调用是成功还是失败。你不能指望它同步行为,因为它永远不会,即使后端立即响应。

这就是为什么你不能期望在你的其他控制器中拥有所需的数据并且需要相应地调整你的代码,例如。

// tells angular to execute watcher function on every $digest
$scope.$watch(function () {
    // watches the returned value of the service's method
    return userDetailsFactory.getUserDetailsFromFactory();
}, function (newVal) {
    // filtering out empty object
    if (!Object.keys(newVal).length) {
        return;
    }

    // the following code will be run when
    // userDetailsFactory.getUserDetailsFromFactory
    // returns some value
    // in our particular case - after getting
    // data from backend and setting it in
    // userDetailsFactory.setUserDetailsFromFactory

    $scope.loading = false;
    $scope.ipForHttp = newVal.ipAddress;
    $scope.ClientID = newVal.clientidFromSession;

    $scope.getDevice();
},
// tells angular to watch properties of the object too
true);

或者,如果您需要在设置用户详细信息时仅执行一次该代码,请执行此操作

var unbindUserDetailsWatcher = $scope.$watch(function () {
    return userDetailsFactory.getUserDetailsFromFactory();
}, function (newVal) {
    if (!Object.keys(newVal).length) {
        return;
    }

    unbindUserDetailsWatcher();
    unbindUserDetailsWatcher = null;

    ...
});