[英]AngularJS. Can't change element in directive
AngularJS 1.5.0. AngularJS 1.5.0。 I'm using custom directive
ddTextCollapse
to collapse\\expand long posts in thread and recently encountered strange problem - some posts won't expand due to DOM not being changed by script. 我正在使用自定义指令
ddTextCollapse
来折叠\\展开线程中的长帖子,最近遇到了奇怪的问题-由于DOM未被脚本更改,因此某些帖子无法展开。 Here's code: 这是代码:
link: function(scope, element, attrs) {
scope.doc.collapsed = false;
var child = element.children();
scope.doc.toggle = function() {
scope.doc.collapsed = !scope.doc.collapsed;
child.toggleClass('collapsed');
console.dir(child); //everything is fine! Should have changed DOM also, but alas.
};
attrs.$observe('ddTextCollapseMaxHeight', function() {
var maxHeight = scope.$eval(attrs.ddTextCollapseMaxHeight);
if (child.height() > maxHeight) {
child.addClass('collapsed');
scope.doc.collapsed = true;
var toggleButton = $compile('<i class="material-icons collapse-text-toggle" ng-click="doc.toggle()">more_horiz</i>')(scope);
child.after(toggleButton);
}
});
}
I'm really confused about why that can happen. 我真的很困惑为什么会发生这种情况。 It seems to depend on the type of post but the problem occurs even when the markup is identical.
它似乎取决于帖子的类型,但是即使标记相同,也会出现问题。 The markup:
标记:
<div ng-if="discus.expanded[doc.unid] !== 'attached'" dd-text-collapse dd-text-collapse-max-height="100" class="col-lg-24 martop5 text-content">
<div ng-bind-html="doc.body | linkCreate | nl2br | quoteAuthor | trustAsHtml" class=""></div>
</div>
It looks like when you messed up with scopes and change child only when you really need to change the parent. 看起来当您弄乱了范围并仅在确实需要更改父级时才更改子级。 But again, I can't see why or how that can happen.
但是同样,我不知道为什么或怎么发生。
If you want you can download the angular js custom directive for this task. 如果需要,可以下载此任务的angular js自定义指令。 There are many directives but I have used the below and it works fine.
有很多指令,但是我使用了下面的指令,效果很好。
http://ngmodules.org/modules/angular-readmore http://ngmodules.org/modules/angular-readmore
我最终只使用了jQuery选择器(而不是element
或child
),并且为此使用它显然是一些Angular错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.