2013-10-04 64 views
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添加第二個方法,這樣你可以使用相同的模型添加第二個'when'與另一個旋轉函數?

編輯: 我加了一些方法(getOrder)後才能出廠,但現在不知道如何調用一個方法specifik:

我試過,但不起作用:

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

回答

1

如果我理解你的權利,應該是這樣的:

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; 
     }  
    } 
}]); 

在我的例子我用同樣的factory有3種類型的promise

  • 初始化
  • 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; 
        }  
    

    }
    }]);

從控制器,你可以稱其爲:

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); 
相關問題