我正在尝试从另一个组件中获取变量的数据,但到目前为止它尚未工作。
的想法是,当我检查复选框时,变量(布尔值(变为真,我的页面上的某些内容可见,反之亦然。
基本上,我希望InstellingenComponent
的变量能够在AfdelingDetailComponent
中工作。使用@Input()
,这是不起作用的...我变得不确定。
这是我的设置组件:
export class InstellingenComponent implements OnInit {
toonNaam = false;
toonTijd = false;
toonType = false;
toonSanitair = false;
toonKinder = false;
toonSalon = false;
toonKamerNummer = false;
hulpKleur = "red";
behandelKleur = "orange";
volKleur = "green";
leegKleur = "white";
我的设置html :(是的,它是对话框(
<h1 md-dialog-title>Instellingen</h1>
<div md-dialog-content>Welke gegevens wil je zien? </div>
<md-checkbox class="check-margin" [(ngModel)]="toonKamerNummer">Kamernummer</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonNaam">Patiëntnaam</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonTijd">Tijdstip behandeling</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonType">Type behandeling</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonSanitair">sanitair</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonKinder">Kinderverzorgingsruimte</md-checkbox>
<md-checkbox class="check-margin" [(ngModel)]="toonSalon">Salon</md-checkbox>
<div md-dialog-actions>
<button md-button md-dialog-close="Option 1">Sluiten</button>
</div>
<app-afdelingdetail
[toonKamerNummer]="toonKamerNummer"
[toonNaam]="toonNaam"
[toonTijd]="toonTijd"
[toonType]="toonType"
[toonSanitair]="toonSanitair"
[toonKinder]="toonKinder"
[toonSalon]="toonSalon"
[behandelKleur]="behandelKleur"
[hulpKleur]="hulpKleur"
[leegKleur]="leegKleur"
[volKleur]="volKleur"
>
</app-afdelingdetail>
带有输入的另一个组件:
export class AfdelingdetailComponent implements OnInit {
@Input() toonNaam:boolean;
@Input() toonTijd:boolean;
@Input() toonType:boolean;
@Input() toonSanitair:boolean;
@Input() toonKinder:boolean;
@Input() toonSalon:boolean;
@Input() toonKamerNummer:boolean;
@Input() hulpKleur;
@Input() behandelKleur;
@Input() volKleur;
@Input() leegKleur;
}
和另一个组件的HTML,可以让您了解我想做什么:
<div class="container" *ngIf="selectedAfdeling"
fxLayout
fxLayout.xs="column"
fxLayoutAlign="center"
fxLayoutGap="10px"
fxLayoutGap.xs="0">
<div *ngFor="let kamer of selectedAfdeling.kamers">
<a [routerLink]="['/patient', kamer.id]">
<div class="kamer" [style.width.px]="kamer.width" [style.height.px]="kamer.height"
[style.background-color]="getColor(kamer)">
<div *ngIf="toonKamerNummer" id="kamernummer">Kamer {{kamer.kamernummer}}</div>
<div *ngIf="toonNaam">{{kamer.naam}}</div>
<div *ngIf="toonType">{{kamer.behandelingstype}}</div>
<div *ngIf="toonTijd">{{kamer.behandelingstijd}}</div>
<div *ngIf="toonSanitair && kamer.sanitair">
<md-icon>wc</md-icon>
</div>
<div *ngIf="toonKinder && kamer.kinderverzorgingsruimte"><md-icon>child_care</md-icon></div>
<div *ngIf="toonSalon && kamer.salon"><md-icon>event_seat</md-icon></div>
</div>
</a>
</div>
</div>
新答案
好吧,显然这是关于您将值与角材料复选框绑定的方式。我尝试了[(ngmodel(],但也失败了。
您应该摆脱[(ngmodel(]绑定(IMHO(,并以这样的方式绑定您的变量:
在您的模板中:
<md-checkbox [checked]="myVariable" (change)="myVariable=!myVariable">Check me!</md-checkbox>
不要忘记在组件中声明和初始化您的变量:
myVariable: boolean = false; // or true
它不会像没有初始化的变量那样工作。
请参阅此plunker :https://plnkr.co/edit/cflsnh2mdreiedxkfnxc?p=peview
感谢这个相关问题:Angular 2复选框两种方式数据绑定
和在此处进行角质材料:https://material.angular.io/components/component/checkbox