Angular 6组件作为应用程序组件中的属性

时间:2018-08-10 18:51:27

标签: components angular6

我正在使用组件作为应用程序组件中的属性,但是当我将该组件放入标记中时,标记的内容为空。

正在用作属性的我的组件:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: '[app-ellipsis-component]',
  templateUrl: './ellipsis-component.component.html',
  styleUrls: ['./ellipsis-component.component.css']
 })
 export class EllipsisComponentComponent implements OnInit {

constructor() { }

ngOnInit() {
 }
 }

我的应用程序组件html

<div class="tile-block" style="width:300px">
<h3 app-ellipsis-component>  
  this is a just name cdsdsc dsdbskcds jsdbkcs cdslnsl;nks cdsn
</h3>
</div>

但是当我运行此代码时, h3 标记内什么也没有显示。 谁能帮助我解决我的问题?

2 个答案:

答案 0 :(得分:1)

对于用作属性的组件,您必须编写:

选择器:“ app-ellipsis-component,[app-ellipsis-component]”,

答案 1 :(得分:0)

实际上,方括号表示法足以用于属性选择器。

在我看来,最初的示例是可行的,例如:

@Component({
  selector: '[app-test]',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {

  @Input('app-test')
  truc: string;

  constructor() { }

  ngOnInit() {
  }

}

并这样使用:

<span app-test="toto">bilou</span>

那么您可能没有在应用程序的任何模块中导入组件:

@NgModule({
  declarations: [
      NavComponent,
      AppComponent,
      TestComponent
  ],
相关问题