How do I include other directives in the template of my customer directive in Angular?

孤者浪人 提交于 2019-12-11 05:05:51

问题


I wrote a simple custom directive. The template in this directive includes other directives (e.g. ui-sortable). Because it doesn't always use ui-sortable, I add it in the link phase. Yet it doesn't seem to apply:

        link: function ($scope,$element,attrs) {
attrs.$observe('admin', function(value) {
  if ($scope.admin) {
        $element.find("span").html("true");
      $element.find("ul").attr("ui:sortable","sortableOptions");
  }
});
    }

Full fiddle example is here: http://jsfiddle.net/VjfEf/4/

There are two lists. The first uses ui-sortable directly and drag/drop/sort works, the second uses my custom members directive. The directive does work, it renders, but the addition of ui-sortable in the exact same way as the first has no impact and drag/drop/sort does not.

I am assuming I am not understanding something about the processing phases of custom directives, and either need to add something to my custom directive?


回答1:


You need to compile the newly added HTML.

$compile($element.contents())($scope);

Fiddle



来源:https://stackoverflow.com/questions/19000285/how-do-i-include-other-directives-in-the-template-of-my-customer-directive-in-an

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