使用Ajax回调重置角度表单
我有一个基本表单,在提交时运行$save操作:使用Ajax回调重置角度表单,ajax,angularjs,callback,coffeescript,scope,Ajax,Angularjs,Callback,Coffeescript,Scope,我有一个基本表单,在提交时运行$save操作: app.controller 'MyThingCtrl', ($scope, MyThing) -> this.addThing = -> new MyThing(this.thing).$save( (data) -> console.log 'New Thing Saved' $scope.things.push data this
app.controller 'MyThingCtrl', ($scope, MyThing) ->
this.addThing = ->
new MyThing(this.thing).$save(
(data) ->
console.log 'New Thing Saved'
$scope.things.push data
this.thing = {}
,(err) ->
console.log 'Error: ' + err
)
我们期望this.thing的表单在成功时会重置,但事实并非如此,因为this不再引用它以前所做的操作。当this的范围在回调中更改时,变量thing可以通过Angular提供的$scope变量获得:
app.controller 'MyThingCtrl', ($scope, MyThing) ->
this.addThing = ->
new MyThing(this.thing).$save(
(data) ->
console.log 'New Thing Saved'
$scope.things.push data
$scope.thing = {}
,(err) ->
console.log 'Error: ' + err
)