本文介绍了在角+打字稿使用templateRequest($ templateRequest不是一个函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我建立这应该采取的html文件,并将其放置在DOM和具有角的编译
编译指令我得到一个错误:
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不是一个函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!