Javascript 错误:没有TemplateRef的提供程序!角4

Javascript 错误:没有TemplateRef的提供程序!角4,javascript,angular,angular5,angular-template,Javascript,Angular,Angular5,Angular Template,我正在尝试学习如何在angular4应用程序中使用template ref,但我似乎不了解文档,我们可以在templateRef中使用来自的视图,但当我尝试实现相同的时,我得到以下错误 错误:没有TemplateRef的提供程序 下面是我的代码示例 import { Component, OnInit, EventEmitter, ViewChild, TemplateRef, ElementRef } from '@angular/core'; import { Observable } fr

我正在尝试学习如何在angular4应用程序中使用template ref,但我似乎不了解文档,我们可以在templateRef中使用来自的视图,但当我尝试实现相同的时,我得到以下错误

错误:没有TemplateRef的提供程序

下面是我的代码示例

import { Component, OnInit, EventEmitter, ViewChild, TemplateRef, ElementRef } from '@angular/core';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(private template: TemplateRef<any>, private element: ElementRef) { }
  ngOnInit() {
    this.template.createEmbeddedView(this.element);
  }
}

<ng-template>
    <div style='border: solid 2px yellow;
    width: 100%;
    height: 50px;
    background-color: #adadad;'>

    </div>
</ng-template>
<div style='border: solid 2px red;
    width: 100%;
    height: 180px;
    background-color: #adadad;'>

</div>
从'@angular/core'导入{Component,OnInit,EventEmitter,ViewChild,TemplateRef,ElementRef};
从“rxjs”导入{Observable};
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
构造函数(私有模板:TemplateRef,私有元素:ElementRef){}
恩戈尼尼特(){
this.template.createEmbeddedView(this.element);
}
}
这里我要实现的是将html放在

更新1

我已将代码更改为此

@ViewChild('tmp1') template: TemplateRef<any>;
  constructor(private element: ElementRef) { }
  ngOnInit() {
       this.template.createEmbeddedView(this.element.nativeElement);
  }


<ng-template #tmp1>
    <div style='border: solid 2px yellow;
    width: 100%;
    height: 50px;
    background-color: #adadad;'>

    </div>
</ng-template>
<div style='border: solid 2px red;
    width: 100%;
    height: 180px;
    background-color: #adadad;'>

</div>
@ViewChild('tmp1')模板:TemplateRef;
构造函数(私有元素:ElementRef){}
恩戈尼尼特(){
this.template.createEmbeddedView(this.element.nativeElement);
}

但是我仍然无法在模板中插入元素,因为您指定了templateURL和styleURL,看起来您使用的是Angular 5,而不是Angular 4。在本例中,您希望将模板/html放入在模板中指定的模板文件中。i、 e.app.component.html

即使在Angular2,4中也有可能。对于AngularJS 4,属性是@Componet内的“模板”。上面的代码使用的是templateUrl,这是AngularJS 5hi@Aravind中的一个新特性,你能看看我的升级问题吗,我仍然无法实现它。这里没有“AngularJS 4”或“AngularJS 5”这样的东西。AngularJS是v1.xuse@ViewChild,用于
私有模板:TemplateRef
而不是构造函数注入