1
尋找訪問組件的值的方法使用<ng-content>
當訪問組件的值:使用時`<ng-content>`
import {Component} from '@angular/core';
@Component({
selector: 'home-page',
template: `<person-box>{{name}}</person-box> <!-- something like this -->`
})
export class HomePageComponent {
// missing code?
}
組件的代碼:
import {Component} from '@angular/core';
@Component({
selector: 'person-box',
template: `<div style="background-color: blue;"><ng-content></ng-content></div>`
})
export class PersonBoxComponent {
name = 'Katharina Muster';
}
(上面的例子是當然非常簡化的)
當使用@ViewChild
它的工作原理:
import {Component} from '@angular/core';
@Component({
selector: 'home-page',
template: `<person-box>{{box.name}}</person-box>`
})
export class HomePageComponent {
@ViewChild(PersonBoxComponent) box: PersonBoxComponent;
}
喜君特。 '@ContentChild(PersonBoxComponent)'不工作(控制檯輸出未定義),但'@ViewChild(PersonBoxComponent)'做。 –
對不起,沒看好班級名字 –
不用擔心班級名稱。 ;)但是你偶然知道,爲什麼'@ ContentChild'不適合我,而是'@ ViewChild'做什麼? –