0

In my app.js I have

$scope.onclick = function(){
         // appending directve "exmDir" to body. 
           document.body.setAttribute('exm-dir', '');
}

The exm-dir is appending to body but it doesn't show its contents. How can I compile? Please help me.

Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
  • Check out: https://docs.angularjs.org/api/ng/service/$compile. Prob in your case $compile(document.body)($scope); – Developer Dec 09 '16 at 11:46
  • In which controller I should write? If I have `mainController` to the body and `dirCntrl` to the `exm-dir`. – Mr_Perfect Dec 09 '16 at 11:49
  • inside `$scope.onclick` method after injecting `exm-dir` directive – Developer Dec 09 '16 at 11:52
  • but the directive content is replacing previous data & not showing even after I removed the directive from the element and recompiled again – Mr_Perfect Dec 09 '16 at 12:17

1 Answers1

0

You can do like

function compile(element){ var el = angular.element(element);
$scope = el.scope(); $injector = el.injector(); $injector.invoke(function($compile){ $compile(el)($scope) })
}

$scope.onclick = function(){ // appending directve "exmDir" to body. var el = document.body; el.setAttribute("exm-dir", ""); compile(el); }

Raj
  • 321
  • 1
  • 4