简体   繁体   English

AngularJS。 无法更改指令中的元素

[英]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选择器(而不是elementchild ),并且为此使用它显然是一些Angular错误。

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

相关问题 Spring MVC,AngularJS。 无法处理JS中的对象值 - Spring MVC, AngularJS. Can't handle object values in JS Angularjs。 嵌套指令中的继承范围 - Angularjs. Inheritance scope in nested directive 使用AngularJs中的指令创建更多/更少阅读切换。 - Create read more/less toggle using directive in AngularJs. 将控制器传递给angularJS中的指令。 给出未定义的错误 - Passing a controller to a directive in angularJS. Gives undefined error AngularJS:使用transclude更改与指令关联的元素 - AngularJS : Change the element associated with directive using transclude 更改angularjs指令链接函数中的元素类 - Change element class in angularjs directive link function 删除数据库条目和AngularJS。 如果无法删除条目,是否应该抛出异常? - Deleting database entries and AngularJS. Should I throw an exception if an entry can't be deleted? 在angularjs中找不到指令创建的postlink中的元素 - Can not find element in postlink of directive creation in angularjs 焦点在angularJS中不起作用。 angular.element(…).focus不是函数吗? - Focus not working in angularJS. angular.element(…).focus is not a function? AngularJS:当父项具有ngIf指令时,为什么不能向祖父母元素添加属性 - AngularJS: Why can't I add an attribute to grandparent element when parent has ngIf directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM