【问题标题】:Angular Form Resetting with Ajax Callbacks使用 Ajax 回调重置 Angular 表单
【发布时间】:2015-06-25 13:36:31
【问题描述】:

我有一个在提交时运行 Angular $save 操作的基本表单:

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 不再指代它之前所做的事情。

【问题讨论】:

    标签: ajax angularjs callback coffeescript scope


    【解决方案1】:

    虽然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
            )
    

    【讨论】:

      猜你喜欢
      • 2011-01-30
      • 1970-01-01
      • 2021-05-07
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      相关资源
      最近更新 更多