带有飞行前请求的Angularjs $ q.all在OPTIONS请求后解析

时间:2016-03-11 05:03:08

标签: angularjs promise angular-promise preflight

我有多个删除请求,前面有OPTIONS转机前请求。但是控件在$q.all请求之后进入OPTIONS。这是我的代码:

companyMasterC.confirmDelete = function(ev, text) {
     simpleDialog.verifyDelete(ev, text).then(function() {
         companyMasterC.deletePromise = companyMasterC.deleteSelected().then(function(response) {
              companyMasterC.getCompanies();
          }, function(error) {
                 console.log(error);
          });
     });
};

companyMasterC.deleteSelected = function(){
   var promises = companyMasterC.selectedRows.map(function(selectedRow) {
       return company.delete({id:selectedRow.id});
   });
   return $q.all(promises);
};

以下是“网络”标签中发生的情况:首先,执行OPTIONS请求,然后执行GET请求companyMasterC.getCompanies(); 执行DELETE请求时应执行的操作。并且在执行GET请求DELETE请求之后。

1 个答案:

答案 0 :(得分:2)

更改

return company.delete({id:selectedRow.id});

return company.delete({id:selectedRow.id}).$promise;

修改后的代码:

companyMasterC.confirmDelete = function(ev, text) {
     simpleDialog.verifyDelete(ev, text).then(function() {
         companyMasterC.deletePromise = companyMasterC.deleteSelected().then(function(response) {
              companyMasterC.getCompanies();
          }, function(error) {
                 console.log(error);
          });
     });
};

companyMasterC.deleteSelected = function(){
   var promises = companyMasterC.selectedRows.map(function(selectedRow) {
       return company.delete({id:selectedRow.id}).$promise;
   });
   return $q.all(promises);
};