我念叨Restangular到处都提到Restangular承诺和角度的作品聪明和模板在以下情况下更新这样的:Restangular失败,诺言和跨域请求
As Angular supports setting promises to scope variables as soon as we get the information from the server, it will be shown in our template
$scope.something = Restagular.one('something').get();
我试图做同样的事情,但Restangular是在服务中,因为我想保持我的控制器清洁。当我做我的REST API的角模板没有更新的请求,我收到此错误:
XMLHttpRequest cannot load
http://localhost:3000/api/template/1
. The request was redirected to'http://localhost:3000/api/template/1/'
, which is disallowed for cross-origin requests that require preflight.
这里是我的代码: 在服务...
myAppServices.service('TemplateService', ['$http', '$cookies', '$cookieStore', Restangular',
function($http, $cookies, $cookieStore, Restangular) {
Restangular.setBaseUrl(constants.serverAddress);
var getTemplate = function(templateId) {
// Check the input
if (!isValidId(templateId))
return;
return Restangular.one('api/template', templateId).get();
};
// Public getters
this.getTemplate = getTemplate;
}]);
在控制器..
$scope.currentCard = TemplateService.getTemplate(1);
那么,在这种情况下,该问题 - 在客户端上或在服务器上。对于我的API,我使用的是django-rest-framework,但是当我使用所有模板(没有特定的ID)获取列表时,我没有问题。
我知道我可以尝试从服务和它的.then()返回一个承诺来设置我的范围变量,但在Restangular的官方回购中提到了这一点,我想使用它,因为代码保持清洁。