Gui*_*man 5 javascript angularjs angularjs-directive angularjs-scope angularjs-resource
当尝试copies在AngularJS资源上轮询自定义方法时,我得到以下错误angular.js:10033:(该方法copy工作正常.)
TypeError: undefined is not a function
at https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:347
at Array.forEach (native)
at q (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:7:280)
at q.then.p.$resolved (https://code.angularjs.org/1.3.0-beta.8/angular-resource.min.js:9:329)
at J (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:101:5)
at J (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:101:5)
at https://code.angularjs.org/1.3.0-beta.8/angular.min.js:102:173
at g.$eval (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:113:138)
at g.$digest (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:110:215)
at g.$apply (https://code.angularjs.org/1.3.0-beta.8/angular.min.js:113:468)
Run Code Online (Sandbox Code Playgroud)
Angular.js 10016 - 10035:
function consoleLog(type) {
var console = $window.console || {},
logFn = console[type] || console.log || noop,
hasApply = false;
// Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
// The reason behind this is that console.log has type "object" in IE8...
try {
hasApply = !!logFn.apply;
} catch (e) {}
if (hasApply) {
return function() {
var args = [];
forEach(arguments, function(arg) {
args.push(formatError(arg));
});
return logFn.apply(console, args); // This is line 10033 where the error gets thrown.
};
}
Run Code Online (Sandbox Code Playgroud)
简化资源:
angular.module('vgm.content-pages')
.factory('ContentPage', function($resource, $http) {
return $resource('/api/content-page/:id', { id:'@page.id' }, {
copy: {
method: 'POST',
url: '/api/content-page/copy/:id'
},
copies: {
method: 'GET',
isArray: true,
url: '/api/content-page/copies/:id'
}
});
});
Run Code Online (Sandbox Code Playgroud)
我收到错误的简化指令:
angular.module('vgm.genericForms')
.directive('vgmFormCopyPicker', function() {
return {
restrict: 'E',
replace: true,
templateUrl: '/static/common/generic-forms/widgets/view-copy-picker.html',
scope: {
resource: '=',
},
controller: function($scope, $element) {
$scope.pages = [];
$scope.loadCopies = function() {
$scope.resource.$copies()
.then(function(response) {
$scope.pages = response.data;
});
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
一旦我运行该loadCopies方法,执行该行就会$scope.resource.$copies()抛出上面的错误.
在Chrome Inspector中,我看到我的API调用实际上已经完成.但解决这个承诺似乎会引发一些错误......
我该如何解决这个错误?
编辑:
$scope.resource = ContentPage.get({id: $stateParams.id}).$promise
$scope.resource.$save() // Works
$scope.resource.$update() // Works
$scope.resource.$copy() // Works
$scope.resource.$copies() // Does not work!
Run Code Online (Sandbox Code Playgroud)
Angular Resource试图用一系列项覆盖我的初始资源.但是Resource的实例push当然没有方法.
我找到了答案:
资源应该在其余生中表示匹配的数据对象.如果要获取新数据,则应使用新对象执行此操作.
$scope.copies = ContentPage.copies()
Run Code Online (Sandbox Code Playgroud)
Guido的答案是正确的但我没有在第一时间得到它.
如果您向Angular添加自定义方法$resource并使用isArray: true并期望从WebService获取某个数组,您可能希望将响应存储在数组中.
因此,您不应该像这样使用实例方法:
var ap = new Ansprechpartner();
$scope.nameDuplicates = ap.$searchByName(...);
Run Code Online (Sandbox Code Playgroud)
但是直接使用资源:
$scope.nameDuplicates = Ansprechpartner.searchByName(...)
Run Code Online (Sandbox Code Playgroud)
使用以下Angular资源:
mod.factory('Ansprechpartner', ['$resource',
function ($resource) {
return $resource('/api/Ansprechpartner/:id',
{ id: '@ID' },
{
"update": { method: "PUT" },
"searchByName": { method: "GET", url: "/api/Ansprechpartner/searchByName/:name", isArray: true }
}
);
}
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16094 次 |
| 最近记录: |