0
angular.module('mymodule', [
])
  .controller('myController', [
      '$scope', 'myModel'
      function($scope, myModel) {
          // Some code here

      }
  ]);
  .factory('myModel', [
      '$scope',
      function($scope) {
          // Some code here

      }
  ]);

然后在您的路由文件中

$routeProvider.when('/someroute', {
    templateUrl: 'partials/someroute.html', 
    resolve: ['myModel'] //ALWAYS IN ARRAY)
});

在此代码中,您在 myModel 中有一个解析方法。有没有办法向 myModel 添加第二个方法,这样您就可以使用相同的模型添加另一个旋转函数的第二个“何时”?

编辑:我向工厂添加了一些方法(getOrder),但现在不知道如何调用特定方法:

我试过这个但不起作用:

  when('/order/:id', {
                        templateUrl: 'partials/order/Order.html',
                        controller: "test.controller.order.Order",
                        resolve: {
                            getOrder: ["test.resolver.Order", function (resolveProvider) {
                                return resolveProvider().getOrder();
                            }
                            ]}
4

1 回答 1

1

If I understand you right, It should be something like:

myModule.factory('ajax_post', ['$http',  function(_http) {   

    return{
        TypeOne: function(jsonData){
            var _promise= _http.post(/**/ );            
            return _promise; 
        },       
        TypeTwo: function(uploadedFile){

            var upload_promise =  _http.post(/**/);

            return upload_promise;
        },
        TypeThree: function(uploadedFile){

            var upload_promise =  _http.post(/**/);

            return upload_promise;
        }     
    }   
 }]);

In my example I use the same factory with 3 types of promise:

  • init
  • uploadFile_init
  • uploadFile_init_for_compare

    myModule.factory('ajax_post', ['$http',  function(_http) {   
    
    var path = 'src/php/data.ajax.php';
    
    return{
        init: function(jsonData){
            var _promise= _http.post(path, 
                jsonData
                ,{
                    headers: {
                        'SOAPActions': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'
                    }
                }
                );            
            return _promise; 
        },       
        uploadFile_init: function(uploadedFile){
            var fd = new FormData();
            fd.append("uploadedFile", uploadedFile);
            var upload_promise =  _http.post("src/php/data.ajax.php",
                fd,
                {
                    headers:{
                        'Content-Type':undefined
                    },
                    transformRequest:angular.identity
                });
    
            return upload_promise;
        },
        uploadFile_init_for_compare: function(uploadedFile){
            var fd = new FormData();
            fd.append("uploadedFileForCompare", uploadedFile);
            var upload_promise =  _http.post("src/php/data.ajax.php",
                fd,
                {
                    headers:{
                        'Content-Type':undefined
                    },
                    transformRequest:angular.identity
                });
    
            return upload_promise;
        }     
    

    }
    }]);

From controller you can call it as:

var promise_1 = ajax_post.init(someJsonData);
var promise_2 = ajax_post.uploadFile_init(someJsonData)
var promise_3 = ajax_post.uploadFile_init_for_compare(someJsonData);
于 2013-10-04T11:31:41.307 回答