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承诺的推荐方法_Javascript_Angularjs - Fatal编程技术网

Javascript 处理服务和控制器之间的AngularJS承诺的推荐方法

Javascript 处理服务和控制器之间的AngularJS承诺的推荐方法,javascript,angularjs,Javascript,Angularjs,下面显示的代码适用于我的目的,但我想确认这是否是最佳实践 我希望我的工厂在第一个实例中对配置文件信息发出http请求,然后在后续请求中简单地重用这些数据 ProfileService.js app.factory('ProfileService', ['$http', '$q', function($http, $q) { var profile; return { getProfile: function() { if (profile

下面显示的代码适用于我的目的,但我想确认这是否是最佳实践

我希望我的工厂在第一个实例中对配置文件信息发出http请求,然后在后续请求中简单地重用这些数据

ProfileService.js

app.factory('ProfileService', ['$http', '$q', function($http, $q) {
    var profile;

    return {
        getProfile: function() {
            if (profile === undefined) {
                return $http.get('url/to/profile.json').then(function(response) {
                    profile = response;

                    return profile;
                });
            }

            return $q.when(profile);
        }
    };
}]);
我有两个控制器,然后利用ProfileFactory访问这些数据的元素

HomeController.js

app.controller('HomeController', ['ProfileService', function(ProfileService) {
    var self = this;

    ProfileService.getProfile().then(function(result) {
        self.name = result.name;

        self.showAlert = result.showAlert;
    });
}]);
app.controller('ProfileController', ['ProfileService', function(ProfileService) {
    var self = this;

    ProfileService.getProfile().then(function(result) {
        self.profile = result;

        self.profile.showAlert = false;
    });
}]);
ProfileController.js

app.controller('HomeController', ['ProfileService', function(ProfileService) {
    var self = this;

    ProfileService.getProfile().then(function(result) {
        self.name = result.name;

        self.showAlert = result.showAlert;
    });
}]);
app.controller('ProfileController', ['ProfileService', function(ProfileService) {
    var self = this;

    ProfileService.getProfile().then(function(result) {
        self.profile = result;

        self.profile.showAlert = false;
    });
}]);

对这种方法的任何反馈都将不胜感激。

我认为这是最好的方法。让服务返回控制器使用的承诺是最好的分离关注点。是的,在服务中使用http调用是最好的。传统上,我们持有的是承诺,而不是数据,如下所示:

app.factory('ProfileService', ['$http', function($http) {
    var profilePromise;

    return {
        getProfile: function() {
            if (profilePromise === undefined) {
                profilePromise = $http.get('url/to/profile.json');
            }
            return profilePromise;
        }
    };
}]);

是的,这正是使用帮助
$q.when
缓存数据的最佳方式。谢谢@koolunix。我开始采用这种方法,然后遇到了一些问题,因为我有多个控制器想要共享和操作响应数据,这导致我将其存储在工厂中。