gpt4 book ai didi

javascript - $compile 显示未捕获的 TypeError : undefined is not a function

转载 作者:行者123 更新时间:2023-11-28 00:26:14 25 4
gpt4 key购买 nike

我正在使用以下代码。以特定元素为目标并附加 <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);


}

最佳答案

试试这个代码。

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

关于javascript - $compile 显示未捕获的 TypeError : undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29469476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com