在角+打字稿使用templateRequest($ templateRequest不是一个函数) [英] Using templateRequest in angular + typescript ($templateRequest not a function)
问题描述
我建立这应该采取的html文件,并将其放置在DOM和具有角的编译
编译指令我得到一个错误:
$ templateRequest不是一个函数
块引用>显然,我做错了什么,不知道是什么,
这是我的指令:
模块Uni.Directives { 出口类uniTable实现ng.IDirective { 公共限制:字符串='EA'; 公共链接:功能=(范围:ng.IScope,
$ templateRequest:ng.ITemplateRequestService,
$编译:ng.ICompileService,
元素:ng.IAugmentedJQuery,
ATTRS:ng.IAttributes)=> { $ templateRequest(template.html,假)。然后(功能(HTML){
VAR模板= angular.element(HTML);
element.append(模板);
$编译(模板)(范围);
});
}
} 角
.module('TModule')
.directive('uniTable',[()=> {返回新Uni.Directives.uniTable()}]);
// ********结束加入模块**********}
解决方案的第二个参数
链接
- 函数中的元素。如果you're试图注入$ templateRequest
和$编译
,你需要做的是在构造函数中:出口类uniTable实现ng.IDirective {
构造函数(私人$ templateRequest:ng.ITemplateRequestService,私人$编译:ng.ICompileService){ }
公共限制:字符串='EA';
公共链接:功能=(范围:ng.IScope,
元素:ng.IAugmentedJQuery,
ATTRS:ng.IAttributes)=> { 这一点。$ templateRequest(template.html,假)。然后(功能(HTML){
VAR模板= angular.element(HTML);
element.append(模板);
$编译(模板)(范围);
});
}
}角
.module('TModule')
.directive('uniTable',['$ templateRequest','$编译',($ templateRequest,$编译)=> {返回新Uni.Directives.uniTable($ templateRequest,$编译)}]);我建议出厂功能,如指令功能打交道时使用的功能。继这结构:
函数uniTable($ templateRequest:ng.ITemplateRequestService,$编译:ng.ICompileService):ng.IDirective {
返回{
限制:EA,
链接:功能(){
$ templateRequest()// doStuff
}
};
}
。uniTable $注射='$ templateRequest','$编译'];
angular.module('TModule')
.directive('uniTable',uniTable);I'm building a directive which supposed to take html file, place it in dom and compile it with angular's compile
I'm getting an error:
$templateRequest is not a function
Clearly I am doing something wrong, don't know what,
This is my directive:
module Uni.Directives { export class uniTable implements ng.IDirective { public restrict: string = 'EA'; public link: Function = (scope: ng.IScope, $templateRequest: ng.ITemplateRequestService, $compile: ng.ICompileService, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => { $templateRequest("template.html",false).then(function (html) { var template = angular.element(html); element.append(template); $compile(template)(scope); }); } } angular .module('TModule') .directive('uniTable', [() => { return new Uni.Directives.uniTable() }]); // ******** End adding to module ********** }
解决方案The second parameter to
link
-function is element. If you´re trying to inject$templateRequest
and$compile
you need to do it in the constructor:export class uniTable implements ng.IDirective { constructor(private $templateRequest: ng.ITemplateRequestService, private $compile: ng.ICompileService){ } public restrict: string = 'EA'; public link: Function = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) => { this.$templateRequest("template.html",false).then(function (html) { var template = angular.element(html); element.append(template); $compile(template)(scope); }); } } angular .module('TModule') .directive('uniTable', ['$templateRequest','$compile',($templateRequest,$compile) => { return new Uni.Directives.uniTable($templateRequest,$compile) }]);
I'd suggest using function when dealing with factory functions like the directive function. Following this structure:
function uniTable($templateRequest: ng.ITemplateRequestService, $compile: ng.ICompileService): ng.IDirective{ return { restrict: 'EA', link: function(){ $templateRequest()//doStuff } }; } uniTable.$inject = ['$templateRequest', '$compile']; angular.module('TModule') .directive('uniTable', uniTable );
这篇关于在角+打字稿使用templateRequest($ templateRequest不是一个函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!