我有这样的服务(为简洁(和隐私)而修改):
angular.module('services', [])
.factory('obj', function ($http) {
return {
get: function () {
return $http.get('/obj').then(function (response) {
return response.data;
});
}
}
});
它的用法如下:$scope.obj = obj.get();
从服务器检索对象。
我可以显示该对象并修改它的大部分键,但我似乎无法修改任何包含数组或其他对象的键。我收到类型错误:
TypeError: Cannot call method 'indexOf' of undefined
我正在尝试添加存在于 promise 对象中的数组的内容。我可以查看它们,只是当我尝试添加或删除它们时,angular 认为它是未定义的。
为什么会这样?
最佳答案
在您的 Controller 中使用 .then
并在回调中执行逻辑:
obj.get().then(function(data) {
$scope.obj = data;
//do stuff
});
关于javascript - 修改 Angular promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528047/