Angular Form Resetting with Ajax Callbacks

谁都会走 提交于 2019-12-11 14:37:02

问题


I have a basic form with an Angular $save action run on submit:

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
        )

The expectation is that the form for this.thing will be reset on success, but it isn't, as the this no longer refers to what it did before.


回答1:


While the scope of this is changed inside the callback, the variable thing is available via the $scope variable provided by Angular:

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
        )


来源:https://stackoverflow.com/questions/29720585/angular-form-resetting-with-ajax-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!