Angularjs custom directive highlight text after ng repeat has run

烂漫一生 提交于 2019-12-24 16:11:52

问题


I want to create a directive that hightlights text( using jquery highlight plugin), after ngRepeat has rendered all its items. Consider following html:

<div data-hightlight="" data-highlight-key="{{vm.highlight}}">
  <div data-ng-repeat="item in items">
       {{item.description}}
  </div>
</div>

If I put the highlight on the outer div, it is executed before the ng repeat has rendered, so nothing gets highlighted. If I put it on the inner div, the directive runs for each item which is highly inefficient.

I also tried checking on the $scope.$last, but then the element to which to linking function is refererring, is only the last item in the list, so only this item gets highlighted text. (I could try and get the parent element, but to me, that doesn't seem to be the best practise)

The code is inside the link function, where I watch changes on the highlightKey attribute.

P.S. vm.highlight is only set to its value after the data returns from the server

来源:https://stackoverflow.com/questions/19880091/angularjs-custom-directive-highlight-text-after-ng-repeat-has-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!