gpt4 book ai didi

javascript - 我必须编码 $apply 才能让我的 ng-repeat 工作,有更好的方法吗?

转载 作者:行者123 更新时间:2023-12-03 12:40:20 25 4
gpt4 key购买 nike

我有一个将数据添加到 $scope 对象的函数。

function newUT(unit, apply) {
newGuid(function (data) {
var newTask = {
id: data,
TaskName: 'New Task',
_showdetails: true,
};
unit.tasks.push(newTask);
apply();
});
}

该函数通过以下方式分配给 Controller 中的$scope $scope.newUT = newUT;

在我放入 array 对象之前,ng-repeat 没有更新。 newGuid 是一个旧版应用程序,用于从服务器获取 Guid,然后将其放入本地文件存储 Guid 索引表中。所以我不能完全消除该代码。

此代码调用者:

<button type="button" ng-click="newUT(unit,$apply)">

所以问题是我真的不喜欢传递$apply,但如果我不这样做,我就无法更新数组。

  1. 有更好的方法吗?
  2. 我看到了Angular service containing http get call not working with ng-repeat
    1. 但我不确定如何将 newGuid 调用包装为 q 调用
    2. 由于 newGuid 是共享服务器调用,我不知道这是否是正确的方法。

还有更好的办法吗?我所做的事情可以接受吗?

最佳答案

$timeout服务可以在这里提供帮助。如果您为 newGuid 方法设置了包装服务,那么您可以将回调包装在 $timeout 中:

JSFiddle

app.service('svc', function($timeout){
this.getGuid = function(cb){
getGuid(function(value){
$timeout(function(){
cb(value);
});
});
};
});

关于javascript - 我必须编码 $apply 才能让我的 ng-repeat 工作,有更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23554742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com