0

My trying to create dynamically an element and during the creation attach an ng-click to it. I know I need to compile that element, only it isn't working. I read here but not able to make it work with my element.

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

function foo(){
    var element = angular.element("<"+element+" ng-click='x()' placeholder='input'>");
    compile(element);
}
5
  • 1
    Check this out Commented Jan 23, 2018 at 20:23
  • Is this in a directive? Commented Jan 23, 2018 at 20:26
  • No, in a function.. I didnt separate the code properly here. Please see edit. Commented Jan 23, 2018 at 20:27
  • function where though? if in directive or controller have access directly to scope Commented Jan 23, 2018 at 20:49
  • 1
    @lealceldeiro comment was very helpful. Thank you all. Commented Jan 23, 2018 at 20:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.