角度误差:对象有没有方法推 [英] angular error : object has no method push
问题描述
我试图做一个角度工作,REST服务和 $资源
。
I am trying to make a angular work with a REST service and $resource
.
它的工作原理,从JSON获得DATAS,但蒙山 $更新保存()
或调用自定义方法 $ REC()时code>,我在控制台的错误说:
类型错误:对象#< C>没有方法推
。开关 IsArray的
到真正
或假
,并没有改变任何东西。
it works to GET the datas from JSON, but when updating whith $save()
or a custom method called $rec()
, i have an error in the console saying : TypeError: Object #<c> has no method 'push'
. Switching isArray
to true
or false
, didn't change anything.
我犯了一个plunker: http://plnkr.co/edit/fS2bjRKPgUulTbTxgg2j
i made a plunker : http://plnkr.co/edit/fS2bjRKPgUulTbTxgg2j
当你做你自己的服务器上运行的错误是可见的。
the error is visible when you make it run on your own server.
在html:
<div ng-controller="TestCtrl">
<div ng-repeat="obj in objs">
<div>
<h3>{{obj.name}}</h3>
<h3>{{obj.age}}</h3>
<input type="text" ng-model="obj.name" /><br />
<input type="text" ng-model="obj.age" /><br />
<button ng-click="save(obj)">Save</button>
</div>
</div>
</div>
和JavaScript的:
and the javascript:
'use strict';
angular.module('TestApp', ['TestApp.services', 'TestApp.controllers']);
angular.module('TestApp.services', ['ngResource']).
factory('Obj', function($resource){
return $resource('datas.json', {}, { 'rec': {method: 'POST', isArray: true } });
});
angular.module('TestApp.controllers', []).
controller('TestCtrl', ['$scope', 'Obj', function($scope, Obj) {
$scope.objs = Obj.query();
$scope.save = function(obj) {
obj.$save();
console.log(obj);
}
}]);
你知道错误从何而来?
do you know where the error comes from ?
推荐答案
我抄你的plnkr到我的dev的机器上,建立一个网站西纳特拉下面,但我还没有看到这个错误。在客户的帖子JSON字符串如 {名=&gt;中露易丝,年龄=&GT;32}
当我点击一个蓝色的按钮,一切似乎都很好。你处理 POST
是否正确?
I have copied your plnkr to my dev machine, set up a sinatra site as below but I still do not see this error. the client posts a JSON string like {"name"=>"louise", "age"=>"32"}
when I click on a blue button, and everything seems fine. Are you handling the POST
appropriately?
这是我的服务器code:
here is my server code:
app.ru
# -*- coding:utf-8; mode:ruby; -*-
require 'json'
require 'multi_json'
require 'sinatra/base'
require 'sinatra/json'
class App < Sinatra::Base
helpers Sinatra::JSON
get '/datas.json' do
json([
{"name" => "louise", "age" => "32"},
{"name" => "jeanne", "age" => "25"},
{"name" => "renée", "age" => "21"},
{"name" => "fernande", "age" => "28"}
])
end
post '/datas.json' do
data = JSON.parse request.body.read.to_s
200
end
end
config.ru
# -*- coding:utf-8; mode:ruby; -*-
require './app.rb'
run App
这篇关于角度误差:对象有没有方法推的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!