Using templateRequest in angular + typescript ($templateRequest not a function)

前端 未结 2 1952
清歌不尽
清歌不尽 2020-12-22 09:52

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:

$te

2条回答
  •  不思量自难忘°
    2020-12-22 10:29

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

提交回复
热议问题