【问题标题】:$scope.push() does not update ng-repeat after http post success$scope.push() 在 http post 成功后不更新 ng-repeat
【发布时间】:2016-05-19 10:50:45
【问题描述】:

我正在尝试在 $http.post 成功后更新视图上的 DOM (ng-repeat)(调用 $http.get),但是它不起作用。

我确实使用 ng-click 调用我的 $http.get 进行了测试,它确实更新了 DOM,所以我很困惑发生了什么。

这是我的 $http.post:

 $scope.save = function(){
    //url, data and config here...
    $http.post(url, data, configPost).then(function(data) {
    if(data.status == 200){

      if ($scope.formData.comment) {
        $scope.formData.comment = '';
        $scope.response.push(initget()); // this is where I'm calling the $http get
      }

    } else {
      console.log('post error');
    }
  });
};

我使用以下函数进行了测试,它在 $http.post 之外工作:

$scope.updateDOM = function() {
    $scope.response.push(initget());
};

<a href="#" ng-click="updateDOM()">update dom</a>

提前致谢。

【问题讨论】:

  • 你是说 initget() 返回一个承诺吗?
  • 是的,我正在使用 $http.get('/someUrl', config).then(successCallback, errorCallback);对于我的 get 方法。
  • 我在问题的任何地方都没有看到ng-repeat...请阅读如何创建minimal reproducible example

标签: javascript angularjs angularjs-scope angularjs-ng-repeat


【解决方案1】:

如果我在您提交(发布)一些数据后正确理解它,您想用剩余数据刷新视图。
我想你可以试试:

$http.get('/someUrl', config).then(function(data){$scope.response = data;})

在您的 POST 调用成功回调中,而不是推送。即代替:

$scope.response.push(initget());

附:我假设您在 $scope.response 上应用 ng-repeat。

【讨论】:

    【解决方案2】:

    问题是因为“ng-controller”在html模板中定义了两次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-02
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2016-10-19
      • 2016-06-09
      相关资源
      最近更新 更多