简体   繁体   English

AngularJS ng-repeat未刷新

[英]Angularjs ng-repeat not refreshed

In angularjs I have a kendo ui drop-down list: in my code, when I click on items in the drop-down list, I remove the selected item and add it to a html table (as a row element): 在angularjs中,我有一个kendo ui下拉列表:在我的代码中,当我单击下拉列表中的项目时,我删除了选定的项目并将其添加到html表中(作为行元素):

$scope.optionsDropDownListCatalogs = {
    dataTextField: "Name",
    dataValueField: "Id",
    select: onSelect
};

function onSelect(e) {  
   //Get the selected item
   var item = $scope.dropdownlistCatalogs.dataItem(e.item.index());

   //Remove it from the dropdownlist
   $scope.optionsDropDownListCatalogs.dataSource.remove(item);

   //Add the item in the table datasource
   $scope.products.push(item);
}

In the html page I have a ng-repeat to show the objects inside the $scope.product object. 在html页面中,我有一个ng-repeat来显示$scope.product对象中的对象。

Sometimes the table is updated, sometimes not. 有时表被更新,有时不更新。 If I put $scope.$apply(); 如果我放$ scope。$ apply(); at the end of the function the table is (seems to be) updated correctly. 在函数的末尾,表已正确更新(似乎已更新)。

Why must I execute the $apply() ? 为什么必须执行$apply() The push() doesn't happen in the same digest? push()不会在同一摘要中发生吗?

$apply enables to integrate changes with the digest cycle $ apply允许将更改与摘要周期集成在一起

You can think of the $apply function as of an integration mechanism. 您可以将$ apply函数视为一种集成机制。 You see, each time you change some watched variable attached to the $scope object directly, Angular will know that the change has happened. 您会看到,每次更改直接附加到$ scope对象的某个监视变量时,Angular都会知道更改已发生。 This is because Angular already knew to monitor those changes. 这是因为Angular已经知道要监视这些更改。 So if it happens in code managed by the framework, the digest cycle will carry on. 因此,如果发生在框架管理的代码中,则摘要循环将继续进行。 However, sometimes you want to change some value outside of the Angular world and see the changes propagate normally. 但是,有时您想要在Angular世界之外更改某些值,并看到更改可以正常传播。 Consider this - you have a $scope.myVar value which will be modified within a jQuery's $.ajax() handler. 考虑这一点-您有一个$ scope.myVar值,它将在jQuery的$ .ajax()处理函数中进行修改。 This will happen at some point in future. 这将在将来的某个时刻发生。 Angular can't wait for this to happen, since it hasn't been instructed to wait on jQuery. Angular不能等待这种情况发生,因为尚未指示它必须等待jQuery。 To tackle this, $apply has been introduced. 为了解决这个问题,引入了$ apply。 It lets you to start the digestion cycle explicitly. 它使您可以显式启动消化周期。 However, you should only use this to migrate some data to Angular (integration with other frameworks), but never use this method combined with regular Angular code, as Angular will throw an error then. 但是,您仅应使用此方法将某些数据迁移到Angular(与其他框架集成),而决不要将此方法与常规Angular代码结合使用,因为Angular会抛出错误。

How all of this is related to DOM? 所有这些与DOM有什么关系?

Well, you should really follow the tutorial again, now that you know all this. 好了,既然您已经知道了所有这些,那么您应该再次真正遵循该教程。 The digest cycle will make sure that the UI and the JS code stays synced, by evaluating every watcher attached to the all $scopes as long as nothing changes. 通过评估与所有$ scope关联的每个观察程序,只要没有任何变化,摘要周期将确保UI和JS代码保持同步。 If no more changes happen in the digest loop, then it's considered to be finished. 如果摘要循环中没有更多更改发生,则视为已完成。 You can attach objects to the $scope object either explicitly in the Controller, or by declaring them in {{expression}} form directly in the view. 您可以在Controller中显式地将对象附加到$ scope对象,也可以直接在视图中以{{expression}}形式声明它们。

SRC: READ THIS SRC:请仔细阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM