我想通过使用位于另一个组件中的按钮来显示和隐藏元素。
所以我有一个仪表板,里面有一定数量的房间和标题。
带有app-header和app-chamber的DashboardComponent的HTML:
<app-header></app-header>
<div class="container">
<div class="row">
<app-chamber [kamers]="kamers"></app-chamber>
</div>
</div>
我的ChamberComponent中有* ngIf这个HTML:
<div class="col-sm-6 col-md-4 col-lg-3 cardcol" *ngFor="let kamer of kamers; let i = index">
<md-card class="chamber wit" *ngIf="kamer.patient == null">
<p *ngIf="showId">{{kamer.id}}</p>
</md-card>
</div>
在HeaderComponent中,我有一个需要显示和隐藏元素的按钮:
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
@Input() aList;
dashboardComponent:DashboardComponent;
chamberComponent:ChamberComponent;
constructor(dashboardComponent: DashboardComponent, chamberComponent:ChamberComponent) {
this.dashboardComponent = dashboardComponent;
this.chamberComponent = chamberComponent;
}
ngOnInit() {
}
// THIS GETS CALLED BY A BUTTON CLICK
toggleId(){
this.chamberComponent.toggleId();
}
}
然后我的ChamberComponent代码:
@Component({
selector: 'app-chamber',
templateUrl: './chamber.component.html',
styleUrls: ['./chamber.component.css']
})
export class ChamberComponent implements OnInit {
@Input() kamers;
showId:boolean;
constructor() {
this.showId=false;
}
ngOnInit() {
}
toggleId = () => {
this.showId = !this.showId;
}
}
所以当我点击按钮时,值会发生变化(我在控制台中记录了这一点),但视图没有更新..
当我在我的ChamberComponent中放置一个调用toggleId()函数的按钮时,视图会显示更新并显示或隐藏元素。
但我需要从标题上的按钮切换它。
答案 0 :(得分:11)
@Input() kamers
与模板*ngIf="kamer.patient == null"
之间存在小的不匹配。
kamer
。<md-card class="chamber wit" *ngIf="kamer.patient === null">
<p *ngIf="showId">{{kamer.id}}</p>
</md-card>
<button (click)="toggleId()">Toggle</button>
@Component({
selector: 'material-app',
templateUrl: 'app.component.html'
})
export class AppComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}
@ViewChild
要引用子组件的功能,请使用Plunker (1)
@ViewChild('child') child;
放在父组件<button (click)="child.toggleId()">Toggle</button>
@Component({
selector: 'material-app',
template: `
<material-child #child></material-child>
<button (click)="child.toggleId()">Toggle</button>
`
})
export class AppComponent {
@ViewChild('child') child;
}
<md-card class="chamber wit" *ngIf="kamer.patient === null">
<p *ngIf="showId">{{kamer.id}}</p>
</md-card>
@Component({
selector: 'material-child',
templateUrl: 'app.component.html'
})
export class ChildComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}
@Output()
+ EventEmitter
要扩展先前的设置以使用兄弟组件,您可以
ViewChild
@Component({
selector: 'material-sibling',
template: `
<button (click)="toggle.emit()">Toggle</button>
`
})
export class SiblingComponent {
@Output() toggle = new EventEmitter();
}
@Component({
selector: 'material-app',
template: `
<material-child #child></material-child>
<material-sibling (toggle)="child.toggleId()"></material-sibling>
`
})
export class AppComponent {
@ViewChild('child') child;
}
<md-card class="chamber wit" *ngIf="kamer.patient === null">
<p *ngIf="showId">{{kamer.id}}</p>
</md-card>
@Component({
selector: 'material-child',
templateUrl: 'app.component.html'
})
export class ChildComponent {
kamer = {
patient: null,
id: '1'
};
showId = false;
ngOnInit() {
}
toggleId() {
this.showId = !this.showId;
}
}