1

I have a need to create a composite directive that incorporates separate fully functional directives. One of my component directives adds an element to the dom and that element binds to a value in the component directive's controller. When the composite directive adds the component directive in the compile function, it seems to work but the piece that has the 2 way binding in the component directive does not appear to get compiled and just renders the {{ctrl.value}} string on the screen. I realize this is a bit convoluted so I have included a plunk to help clarify the issue.

 app.directive('compositeDirective', function($compile){
  return {
    compile: compileFunction
  }
  function compileFunction(element, attrs){
    attrs.$set("component-directive", "");
    element.removeAttr("composite-directive");
    element.after("<div>Component value when added in composite directive: {{compCtrl.myValue}}</div>");
    return { post: function(scope, element){
      $compile(element)(scope);
    }};
  }
});
app.directive('componentDirective', function(){
  return {
    controller: "componentController as compCtrl",
    link: link
  };
  function link(scope, element){
    element.after("<div>Component value: {{compCtrl.myValue}}</div>");
  }
});
app.controller('componentController', function(){
  var vm = this;
  vm.myValue = "Hello";
});

http://plnkr.co/edit/alO83j9Efz62VTKDOVgc

4

1 に答える 1