$compile showing uncaught TypeError: undefined is not a function

$compile showing uncaught TypeError: undefined is not a function

我正在使用以下代码。以特定元素为目标并在其中附加一个 <div> 但未捕获 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);


  }

试试这个代码。

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