[英]AngularJS : directive inside ng-repeat not respecting new order
First, here's the issue reproduced in Plunker . 首先, 这是在Plunker中转载的问题 。
Basically I have a directive that will insert some element based on some info inside an ng-repeat
. 基本上我有一条指令,该指令将基于
ng-repeat
内的某些信息插入一些元素。 For my application, I'm using a pretty similar approach where it inserts an <img>
, <video>
, or <audio>
element based on a mime type. 对于我的应用程序,我使用了一种非常相似的方法,即根据mime类型插入
<img>
, <video>
或<audio>
元素。
You'll see that when items are reordered in the array the ng-repeat
is using, the item passed to the pageStyle
directive isn't up to date with the new order anymore. 您会看到,当在
ng-repeat
使用的数组中对项目进行重新排序时,传递给pageStyle
指令的项目不再与新订单保持pageStyle
。 It seems to retain whatever was in its spot before. 它似乎保留了以前的位置。
app.directive('pageStyle', function () {
return {
restrict: 'A',
scope: {
pageStyle: '='
},
replace: true,
link: function (scope, element, attrs) {
var ele;
switch (scope.pageStyle) {
case 'bold':
ele = angular.element('<b>bold</b>');
break;
case 'italics':
ele = angular.element('<i>italics</i>');
break;
case 'strike':
ele = angular.element('<strike>strikethrough</strike>');
break;
}
element.append(ele);
}
};
});
So I suppose my main question is how I get the directive to reevaluate when the order within an ng-repeat
is changed. 因此,我想我的主要问题是,当
ng-repeat
的顺序更改时,如何获得重新评估的指令。
This is because of the use of 'track by'. 这是因为使用了“跟踪依据”。 'track-by' causes not to re-build the dom elements completely, hence improves performance for ng-repeat rendering.
“跟踪”导致无法完全重建dom元素,因此提高了ng-repeat渲染的性能。 http://www.bennadel.com/blog/2556-using-track-by-with-ngrepeat-in-angularjs-1-2.htm
http://www.bennadel.com/blog/2556-using-track-by-with-ngrepeat-in-angularjs-1-2.htm
Your problem requires re-rendering. 您的问题需要重新渲染。 So, remove 'track by'.
因此,删除“跟踪依据”。 You can check the issue by adding watch on pageStyle scope variable.
您可以通过在pageStyle范围变量上添加watch来检查问题。 That will give you clear insight of the issue.
这将使您清楚地了解问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.