0

我正在尝试从另一个组件的变量中获取数据,但到目前为止它还没有工作。

这个想法是,当我选中一个复选框时,变量(布尔值)变为 true,并且我页面上的某些内容是可见的,反之亦然。

所以基本上我希望变量 fromInstellingenComponentAfdelingDetailComponent. 它@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>
4

1 回答 1

1

新答案

好的,显然这是关于使用 Angular Material 复选框绑定值的方式。我尝试了 [(ngModel)] 并且我也失败了。

您应该摆脱 [(ngModel)] 绑定(恕我直言),并像这样绑定您的变量:

在您的模板中:

<md-checkbox [checked]="myVariable" (change)="myVariable=!myVariable">Check me!</md-checkbox>

不要忘记在组件中声明并初始化您的变量:

myVariable: boolean = false; // or true

如果变量未初始化,它将无法正常工作。

看到这个插件:https : //plnkr.co/edit/CFLsnh2MDreiEDXKfnXc ?p=preview

感谢这个相关问题: Angular 2 Checkbox Two Way Data Binding

并在此处查看 Angular Material 文档: https ://material.angular.io/components/component/checkbox

于 2017-06-08T11:32:07.117 回答