2015-04-06 49 views
1

我正在使用以下代码。要针对特定​​的元素,并附加<div>内部的,但得到遗漏的类型错误:未定义不是$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); 


    } 
+0

您可以更深入地了解执行此操作的上下文吗? $ compile和$ scope是否定义? – AlexHv 2015-04-06 10:33:03

+0

你注入了'$ compile'服务吗? – lvarayut 2015-04-06 10:33:14

+0

是的,我注入了它 – 2015-04-06 10:33:40

回答

1

试试这个代码的功能。

$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); 
     }); 
    } 
相关问题