2016-04-07 81 views
0

我一直在面对一个问题很长一段时间。你能帮我解决这个问题吗?其实,我想在很多地方使用table指令。所以我创建了一个指令并将角度UI网格绑定到它。但我得到编译错误。Angular:编译未定义

Error: angular.min.js:102 ReferenceError: compile is not defined

但我可以加载网格内容

我做错了的是什么?

HTML 
<div ng-controller="ctrl1 as two"> 
    <ltcg-table><ltcg-table>  
</div> 
<div ng-controller="ctrl2 as two"> 
    <ltcg-table><ltcg-table>  
</div> 

JS 

myApp.directive('ltcgTable', function($compile) { 
      return { 
       restrict: 'E', 
       transclude: true,  
       replace: false,        
       scope: { }, 
       controller: Controller, 
       controllerAs: 'Controller', 
       bindToController: true,    
       compile: compile 
      } 

     }); 
+0

我不知道它会做什么,而是使用'compile:$ compile',而不是'compile:compile'。 – Roy

+0

谢谢。但在使用编译时:$ compile获取错误angular.min.js:102错误:[$ compile:multidir] http://errors.angularjs.org/1.3.15/$compile/multidir?p0=ltcgTable&p1=uiGrid...p3 =%3Cltcg-table%20ui-grid%3D%22gridOptions%22%20class%3D%22ng-scope%22%3E – klmuralimohan

回答

0

compile函数(的compile: compile秒部分)没有定义。它应该在的形式的函数:

function compile(tElement, tAttrs, transclude) { ... } 

所以尽量定义指令,像这样:

myApp.directive('ltcgTable', function($compile) { 
    return { 
     restrict: 'E', 
     transclude: true,  
     replace: false,        
     scope: { }, 
     controller: Controller, 
     controllerAs: 'Controller', 
     bindToController: true,    
     compile: function(tElement, tAttr, transclude) { 
      //do some stuff here 
     } 
    } 

}); 

,或者,如果你不需要做任何特殊和电网工程因为它是,离开compile领域的方向规范。

+0

感谢您的回复。但没有运气,当我使用编译函数我得到一个错误“angular.min.js:102错误:[$编译:multidir] http://errors.angularjs.org/1.3.15/$compile/multidir?p0 = ltcgTable&p1 = uiGrid&p2 = transclusion&p3 =%3Cltcg-table%20ui-grid%3D%22gridOptions%22%3E“ – klmuralimohan

+0

你真的需要transclude为真吗?你真的需要编译功能吗?你真的需要指令范围吗?如果不是,请将这些字段留在指令配置中。 – fikkatra

+0

是的,我需要隔离的范围,因为我想重用这个指令。 – klmuralimohan