1

I am using the following code. To target the particular element and append a <div>inside that but getting uncaught typeerror: undefined is not a function for $compile(newDirective)($scope);

 $scope.grid= function (targetElement)
  {  
     console.log("target element",targetElement)
     var newDirective = angular.element("<div style='height:200px' > 
     Sample code 
     </div>");


     targetElement.append(newDirective);


     $compile(newDirective)($scope);


  }
4
  • Could you give more insight on the context in which you are executing this ? Are $compile and $scope defined ? Commented Apr 6, 2015 at 10:33
  • Did you inject the $compile service? Commented Apr 6, 2015 at 10:33
  • It means that you didn't inject $compile service. Show full code. Commented Apr 6, 2015 at 10:35
  • function DashboardCtrl($scope, $routeParams, $q, $http, $timeout,$compile) ..... I am adding these line to inject the $scope and $compile... Commented Apr 6, 2015 at 10:37

1 Answer 1

1

Try this code.

 $scope.grid= function (targetElement)
      {  
       var $div = $("<div style='height:200px' >  Sample code </div>");
       $(targetElement).append($div);

       angular.element(targetElement).injector().invoke(function($compile) {
       var scope = angular.element($div).scope();
       $compile($div)(scope);
      });
     }
Sign up to request clarification or add additional context in comments.

Comments

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.