$compile 显示未捕获的 TypeError: undefined is not a function [英] $compile showing uncaught TypeError: undefined is not a function

查看:24
本文介绍了$compile 显示未捕获的 TypeError: undefined is not a function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码.定位特定元素并在其中附加

但未捕获类型错误: undefined is not a function for $compile(newDirective)($scope);

 $scope.grid= 函数 (targetElement){console.log("目标元素",targetElement)var newDirective = angular.element("<div style='height:200px' >示例代码

");targetElement.append(newDirective);$compile(newDirective)($scope);}

解决方案

试试这个代码.

 $scope.grid= 函数 (targetElement){var $div = $("<div style='height:200px' > 示例代码 </div>");$(targetElement).append($div);angular.element(targetElement).injector().invoke(function($compile) {var scope = angular.element($div).scope();$compile($div)(范围);});}

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


  }

解决方案

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

这篇关于$compile 显示未捕获的 TypeError: undefined is not a function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆