Я создал объект ресурса:Angularjs ресурсов a.push Объект не является функцией
factory('TextResource',
function($resource) {
return $resource(adminBaseUrl+'/texts/:type', {}, {
create: {method: 'POST', params: {type:'create'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
update: {method: 'POST', params: {type:'update'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
query: {method: 'GET', params: {type: 'list'}},
remove: {method: 'POST', params: {type: 'remove'}, headers: {'Content-Type':'application/x-www-form-urlencoded'}},
getText: {method: 'GET', params: {type: 'get', id:'@id'}}
});
}
)
И мой контроллер:
controller('EditText', ['$scope', '$location', '$routeParams', 'TextResource', 'HttpStatusMessage',
function($scope, $location, $routeParams, TextResource, HttpStatusMessage) {
$scope.alerts = [];
$scope.languages = [];
TextResource.getText(
{id: $routeParams.id},
function(data) {
$scope.languages = data.result;
},
function(error) {
var httpError = new HttpStatusMessage(error.status);
$scope.alerts.push({type:'error', msg:httpError.msg});
});
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
}
$scope.submit = function() {
TextResource.update(
$scope.languages,
function(data) {
if(data.type == 'success') {
$location.path('texts');
} else {
$scope.alerts.push({type:data.type, msg:data.message});
}
},
function(error) {
var httpError = new HttpStatusMessage(error.status);
$scope.alerts.push({type:'error', msg:httpError.msg});
});
}
$scope.cancel = function() {
$location.path('texts');
}
}
])
Ответ я получаю от запроса TextResource.getText является :
{"result":[{"id":"3","value":"This is my first text<br>","key":"my_first_text","language_id":"1","name":"English"},{"id":"3","value":"Ceci est mon premier texte","key":"my_first_text","language_id":"3","name":"French"}],"num_rows":2}
Теперь, когда я нажимаю на представить его отображает сообщение об ошибке:
Error: a.push is not a function
Объект ответа содержит 2 результата, а результат num_rows - это массив. Причина, по которой я не использую параметр isArray в объекте ресурса, в случае, если какая-либо ошибка возникла на сервере, например, время сеанса, доступ не разрешен и т. Д. Сервер вернул объект, содержит ошибку msg.
Найдите свой код, где находится «a.push»? – DavidLin
a.push написан в файле angularjs –
Я вижу, что ваши действия не определили «isArray», вы попробовали установить его? , isArray - {boolean =} - Если true, возвращаемым объектом для этого действия является массив, см. раздел возврата. – DavidLin