为什么变量在工厂 angular 上一直为空?

why variables keep being empty on factory angular?

services.factory('profilFactory',['$q','$http',function($q,$http){
    var factory2 = 
    {
        profils : {},
        getProfils : function(){
            $dfd = $q.defer();
            $http.get('data.json')
                .success(function(data,status){
                    this.profils = data.profil;
                    $dfd.resolve(this.profils);
                })
                .error(function(data,status) {
                    $dfd.reject('erreur recuperation des profils');
                });
            return $dfd.promise;
        },
        getProfil : function(idProfil){
            var profil={};
            var profils = {};
            factory2.getProfils().then(function(data){
                profils= data;
                console.log(profils);//all right until here profils has values 
            });
            console.log(profils);// now  profils is empty :\ and the foreach will not execute 
            angular.forEach(profils, function(value, key){
                if(value.id == idProfil){
                    profil= value;
                }
            });
            return profil;
        }

    };
    return factory2;
}]);

这是问题的截图:方法"getProfil"

您的 console.log 语句在回调之外。这就是问题所在。您需要在回调中 console.log 或使用观察者在它发生变化时更新它。为了将来参考,您应该始终在此处复制并粘贴您的代码。

回答您的问题,"Why are the variables empty in the factory",这是因为您在尚未从服务器加载数据的位置使用了 console.log 语句。要了解更多信息,Google 这个:"angularjs http get promises"

services.factory('profilFactory',['$q','$http',function($q,$http){
    var factory2 = 
    {
        profils : {},
        getProfils : function(){
            $dfd = $q.defer();
            $http.get('data.json')
                .success(function(data,status){
                    this.profils = data.profil;
                    $dfd.resolve(this.profils);
                })
                .error(function(data,status) {
                    $dfd.reject('erreur recuperation des profils');
                });
            return $dfd.promise;
        },
        getProfil : function(idProfil){
            var profil={};
            var profils = {};
            // Run a function to get data, "THEN" we run a function to process the data:
            factory2.getProfils().then(function(data){
                // Data has now been loaded so we can process it and return it.
                profils = data;
                angular.forEach(profils, function(value, key){
                    if(value.id == idProfil){
                        profil= value;
                    }
                });
                return profil;
            });

            console.log(profils); // This is EMPTY because it runs immediately after
            // the factory2.getProfils() function which may need several seconds to
            // load data. That's why "profils" is empty. The data hasn't loaded at
            // this point.
            //
            // No data will be available at this level of the code.  Don't try to access
            // "profils" here!  Only in your .then() function above.
        }

    };
    return factory2;
}]);