Javascript 从$scope承诺中删除一行

Javascript 从$scope承诺中删除一行,javascript,angularjs,angularjs-scope,promise,ng-grid,Javascript,Angularjs,Angularjs Scope,Promise,Ng Grid,Angular足够聪明,可以直接绑定承诺。我有一个ng grid实例,它绑定到从服务返回的承诺。稍后,我想删除该网格中的一条记录,但由于我没有可直接操作的数组,所以不确定如何删除 例如,以下命令将抛出一个错误“TypeError:Object”没有方法“splice”,因为“gridData”实际上不是数组 //Bind promise directly to scope (get returns a promise not an array) $scope.gridData = myServi

Angular足够聪明,可以直接绑定承诺。我有一个ng grid实例,它绑定到从服务返回的承诺。稍后,我想删除该网格中的一条记录,但由于我没有可直接操作的数组,所以不确定如何删除

例如,以下命令将抛出一个错误“TypeError:Object”没有方法“splice”,因为“gridData”实际上不是数组

//Bind promise directly to scope (get returns a promise not an array)
$scope.gridData = myService.get();

//Later I remove a row based on user a user clicking a button on the grid row.
var function = removeRow(rowIdx)
    { 
       $scope.gridData.splice(rowIdx, 1);
    };

最终,我如何将范围值设置为promises并仍然直接操作数据?

有多种方法可以做到这一点。其中之一是编写数组的哪些切片:

.filter("slice", function () {
    return function (array, begin, end) {
        if (!angular.isArray(array)) return;
        return array.slice(begin, end);
    };
});
并将其用于ng内部的收集重复:

<li ng-repeat="item in (data|slice:1)">{{item}}</li>

底层数据数组在
$scope.gridOptions.ngGrid.data中公开(假设您的ngGrid绑定到
gridOptions
)-您可以直接操作它,尽管我没有尝试过此操作,但在解析承诺后您无法更改承诺的值

您可以做的是将实际值设置为范围属性并修改它。事实上,你应该尽快摆脱这个承诺

//Bind promise directly to scope (get returns a promise not an array)
myService.get().then(function (resolved) {
    $scope.gridData = resolved;
});

//Later I remove a row based on user a user clicking a button on the grid row.
function removeRow(rowIdx) { 
   $scope.gridData.splice(rowIdx, 1);
};

我应该说得更清楚些。当用户单击网格中的按钮时,拼接会在将来的某个不确定点发生。这意味着我用完整的数据集填充网格。然后,应该将slice:start与$scope.start==undefined一起使用,而不是slice:1。当用户想要分割行时,您可以更改起始值,视图将自动更新。我把第一个例子改成这样,看看。是的。仅对“仅显示”数据使用承诺。。。否则,实际上会将值绑定到范围。
//Bind promise directly to scope (get returns a promise not an array)
myService.get().then(function (resolved) {
    $scope.gridData = resolved;
});

//Later I remove a row based on user a user clicking a button on the grid row.
function removeRow(rowIdx) { 
   $scope.gridData.splice(rowIdx, 1);
};