【发布时间】:2013-12-08 03:45:06
【问题描述】:
我在$scope.raw 中有一个食品列表,我想在列中显示这些数据,所以我稍微改变了结构。我在sortStuff() 函数中执行此操作,并将更新的数据存储在$scope.allfood 中。每次$scope.raw 发生任何变化时,都会有一个 $watch 调用sortStuff()(我正在使用拖放来更改食物类别):
$scope.$watch('raw', function(){
$scope.allfood = $scope.sortStuff();
console.log($scope.allfood);
}, true);
当食物被拖来拖去时会发生这种情况:
receive:function(event, ui) {
var issueScope = angular.element(ui.item).scope();
scope.$apply(function() {
var recp = _.find(scope.raw, function(lineitem){
return lineitem.name === issueScope.receipe.name;
})
recp.cat = scope.col.name;
})
$(ui.item).remove(); // remove DOM
}
基本上,我在$scope.raw 中搜索正确的对象并将cat 更改为食物的新类别。我还删除了 dom 元素,因为我指望 ng-repeat 来刷新视图。这似乎工作正常:$watch 中的 console.log 显示对象正在移动到正确的类别,并且数据看起来应该是什么样子。然而,从视觉上看,ng-repeat 并没有反映数据。
将项目从 B 拖动到 C 可以正常工作。将一个从 A 拖到 B,使 B 中的两个项目消失...结果非常不一致,我不知道发生了什么。
有什么想法吗?或者有什么更好的方法来做到这一点的建议?
【问题讨论】:
-
你应该使用 $watchCollection('raw', function() {});但我已经这样做了,但在我的应用程序中仍然看到同样的问题......
标签: javascript angularjs angularjs-directive angularjs-ng-repeat