简体   繁体   English

角度2:带有ngif的内容未删除

[英]angular 2: content with ngif not getting removed

I have a section of my html to only be visible if consult.comeback . 我的html部分只有在consult.comeback可见。 In my constructor I call 在我的构造函数中,我称

export class InviteConsultModal {
  @ViewChild(Slides) slides: Slides;
  consult: any = {};
  employees: any = [];

  constructor( private viewCtrl: ViewController, private storage: Storage, private params: NavParams) {

    this.consult.comeback = false;

  }
}

and then my html looks like so: 然后我的html看起来像这样:

<ion-item>
  <ion-label floating>Is this a comeback?</ion-label>
  <ion-select required name="comeback" [(ngModel)]="consult.comeback">
    <ion-option value="true">Yes</ion-option>
    <ion-option value="false">No</ion-option>
  </ion-select>
</ion-item>
<ion-item>
  <!-- displays correct value -->
  {{consult.comeback}}
</ion-item>

<!-- initially is NOT shown -->
<ion-item *ngIf="consult.comeback">
  <ion-label floating>Original Sale</ion-label>
  <ion-select [(ngModel)]="consult.original" name="original">
    <ion-option *ngFor="let employee of employees" [value]="employee.id">{{ employee.name }}</ion-option>
  </ion-select>
</ion-item>

The strange thing is that after I change the value of consult.comeback to true , the section appears (as expected), but once I change it back to false , it does not go away. 奇怪的是,在将consult.comeback的值更改为true ,该部分出现了(如预期的那样),但是一旦将其更改为false ,它就不会消失。

How can I make the visibility toggle with the value? 如何使可见度与值切换?

option tag values are strings so your options are really "true" "false" . 选项标签值是字符串,因此您的选项实际上是"true" "false" So *ngIf="'false'" is technically a true condition. 因此*ngIf="'false'"从技术上讲是一个真实的条件。

You could switch up the *ngIf to 您可以将* ngIf切换到

<ion-item *ngIf="consult.comeback === 'true'">
</ion-item>

Here is a plunkr demoing this. 这是一个演示者。 https://plnkr.co/edit/YthqofKZP7LcG6zY0mVK?p=preview https://plnkr.co/edit/YthqofKZP7LcG6zY0mVK?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM