智能表(AngularJs)异步数据更改未更新

时间:2016-11-23 21:52:15

标签: angularjs ajax smart-table

我有一张桌子,使用AngularJs中的Smart Table制作。

我正在尝试获取新数据并使用ajax添加到此表。我在ajax回调中推送到$scope.rowCollection。但是为什么新添加的数据没有显示在表格中? (我正在使用st-safe-src,我正在st-safe-src向集合中添加新数据。)

另一个问题是:每次更改$scope.displayedCollection = [].concat($scope.rowCollection);时是否必须添加st-safe-src? (添加此行不能解决问题)

我使用超时创建了此Plunkr来模拟ajax回调。

谢谢!

1 个答案:

答案 0 :(得分:1)

我通过更新超时代码修复了你的plunkr,以便调用$scope.$apply。您应该使用$scope.$apply来确保在使用非角度AJAX调用(如jQuery)或核心JS回调(如jQuery)时,通知角度JS组件已更改角度JS模型:{{3 }}

      // use $timeout service so that we can automatically invoke 
      // the appropriate apply
      $timeout(function () {
        $scope.rowCollection.push({firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com'});
        // do I need this?
        $scope.displayedCollection = [].concat($scope.rowCollection);
        console.log("executed");
      }, 2000, true);

你也可以这样做:

      setTimeout(function () {
        $scope.$apply(function () {
          $scope.rowCollection.push({firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever@gmail.com'});
          // do I need this?
          $scope.displayedCollection = [].concat($scope.rowCollection);
        });
        console.log("executed");
      }, 2000);