简体   繁体   English

NgModel 只显示一个值

[英]NgModel shows only one value

在此处输入图像描述 在此处输入图像描述 Hello I am calling a column values from db (interest) and i want to pass them with NgModel to ion-toggles.您好,我正在从 db (interest) 调用列值,我想将它们与 NgModel 一起传递给 ion-toggles。 As you may see on the screenshot 3 values are returning (Painting, Illustration, Graphic Design) but only 1 (Painting) is true (toggle on right side)正如您在屏幕截图中看到的那样,正在返回 3 个值(绘画、插图、平面设计),但只有 1 个(绘画)为真(在右侧切换)

Html Html

<ion-list >
    <ion-list-header>
    I am Interested in...
    </ion-list-header><div><br /></div>
    <ion-item>
      <ion-label>Painting</ion-label>
      <ion-toggle color="gold" [ngModel]="interest=='Painting' ? true:false" (ionChange)="creativeInterest(creative, 'Painting')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Graphic Design</ion-label>
      <ion-toggle color="tertiary" [ngModel]="interest=='Graphic Design' ? true:false" (ionChange)="creativeInterest(creative, 'Graphic Design')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Illustration</ion-label>
      <ion-toggle color="danger" [ngModel]="interest=='Illustration' ? true:false" (ionChange)="creativeInterest(creative, 'Illustration')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Sculpture</ion-label>
      <ion-toggle color="success" [ngModel]="interest=='Sculpture' ? true:false" (ionChange)="creativeInterest(creative, 'Sculpture')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Literature</ion-label>
      <ion-toggle color="danger" [ngModel]="interest=='Literature' ? true:false" (ionChange)="creativeInterest(creative, 'Literature')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Theater</ion-label>
      <ion-toggle color="dark" [ngModel]="interest=='Theater' ? true:false" (ionChange)="creativeInterest(creative, 'Theater')"></ion-toggle>
    </ion-item>

    <ion-item>
      <ion-label>Film</ion-label>
      <ion-toggle color="warning" [ngModel]="interest=='Film' ? true:false" (ionChange)="creativeInterest(creative, 'Film')"></ion-toggle>
    </ion-item>

component.ts组件.ts

export class CreativeSettingsPage implements OnInit {
  creativeInterests: any=[];
  creative: any= {};
  userDetails: any = {};
  interest:any=[];
  constructor(
    public userData: UserData
  ) {
    this.userDetails = this.userData.getUserData();
   }

  ngOnInit() {
    this.creativeInsterestSet();
  }

  creativeInsterestSet() {
    this.userData.creativeInterests(this.userDetails.uid).pipe(
        map((data: any) => {
            if (data.success) {
                this.creativeInterests = data.creativeInterests;
                this.creativeInterests.filter(item =>  {
                  this.interest=item.interest;
                 console.log( this.interest);
              });
               
            }
        })
    ).subscribe()
}

You should use ngModel with just a variable name such as:您应该只使用带有变量名的 ngModel,例如:

<element [ngModel]="variableName"></element>

and then in your component assign a value to this ngModel based on some logic:然后在你的组件中根据一些逻辑为这个ngModel

assignLogic() {
    if (otherVariable === "Theater") {
        variableName = "Theater";
    }
}

This will update your display!这将更新您的显示!

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

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