简体   繁体   English

如何在AngularJS的许多控制器上仅使用一个$ http请求

[英]How to use an only one $http request on many controllers in AngularJS

I have a file with the name app.js and there, I use many controllers for my web applications. 我有一个名为app.js的文件,在那里,我为Web应用程序使用了许多控制器。 My problem is I repeat many times the same code. 我的问题是我多次重复相同的代码。 For example this $http POST 例如,此$ http POST

 $http({
        method: 'POST',
        url: urlProfile,
        data: $.param($scope.ProfileForm),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Token': '\UsernameToken Token="' + mycookie + '"',
        }
    }).success(function(data, status, headers, config) {
        $scope.showProfiles();
        $('#newProfile').modal('hide');
        $scope.notifications(
            $scope.title = 'Notificación',
            $scope.body = 'Se ha creado un nuevo perfil',
            $scope.icon = '/img/restore.png'
        );
    }).error(function(data, status, headers, config) {
        alert('No se ha guardado el Perfil con el nombre: ' + profilename);
    });

How can I use this method like Service o Factory in my controllers and only provide the params like URL or Method 如何在控制器中使用Service o Factory之类的方法,而仅提供URL或Method之类的参数

Create your own factory and return the promise object: 创建自己的工厂并返回promise对象:

(function(){
    angular.module('test', []);

    angular.module('test')
        .controller('Controller1', ['$scope', 'myService', function($scope, myService){
            myService.myRequest(URL, $.param($scope.ProfileForm)).then(function(result){
                //result of ajax here, do your scope manipulation here
            });
        }])
        .controller('Controller2', ['$scope', 'myService', function(myService){
            myService.myRequest(URL, $.param($scope.ProfileForm)).then(function(result){
                //result of ajax here, do your scope manipulation here
            });
        }])
        .factory('myService', ['$http', function($http){
            return {
                myRequest: function(urlProfile, data){
                    return $http({
                        method: 'POST',
                        url: urlProfile,
                        data: data,
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'X-Token': '\UsernameToken Token="' + mycookie + '"',
                        }
                    }).then(function(result){
                        return result.data;
                    });
                }
            };
        }]);
})();

I recommend following JohnPapa's style-guide . 我建议遵循JohnPapa的样式指南 It is modular and helps you separate your concerns. 它是模块化的,可帮助您分离问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个AngularJS文件中有多少个控制器(服务,工厂,指令) - How many controllers (services,factories,directives) in one AngularJS file 如何在AngularJS中使用Controllers? - How to use Controllers in AngularJS? 如何从Angularjs中的几个控制器使用一个websocket连接 - How to use one websocket connection from few controllers in Angularjs AngularJS-HTTP请求在控制器中发出预检请求,但不在app.js中 - AngularJS - HTTP request makes preflight request in controllers but not in app.js 如何让angularjs控制器只运行一次甚至在DOM中定义的许多ng-controller? - How to let the angularjs controller run only once even many ng-controllers defined in DOM? 如何在angularjs中只发送一个请求而不是多个请求? - how to send only one request instead of multiple request in angularjs? javascript:在不同的 iframe 中加载相同的 url,如何只使用一个 http 请求? - javascript: Loading the same url in different iframes, how to use only one http request? 如何在angularjs控制器中使用来自HTTP请求的数据? - How to use data from http request in angularjs controller? 如何在angularJS中为一个视图定义多个控制器? - How to define multiple controllers for one view in angularJS? 如何使用多个AngularJS嵌套/相关控制器 - How to use multiple AngularJS nested / related controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM