$compile showing uncaught TypeError: undefined is

2019-08-13 01:19发布

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);


  }

1条回答
聊天终结者
2楼-- · 2019-08-13 02:03

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);
      });
     }
查看更多
登录 后发表回答