简体   繁体   English

如何更改 Angular mat-slide-toggle 和 mat-checkbox 上的 VISUAL 状态

[英]How to change VISUAL state on Angular mat-slide-toggle and mat-checkbox

I have an Angular form that has a select box of objects created from database records.我有一个 Angular 表单,它有一个从数据库记录创建的对象选择框。 I want to populate the form with the object values when one is selected.我想在选择一个对象时用对象值填充表单。

My Angular app has an array of objects that is mapped to form control text boxes and mat-slide-toggles.我的 Angular 应用程序有一组对象,这些对象映射到表单控件文本框和 mat-slide-toggles。 My form populates with the correct values from the array and when I click submit the correct values show.我的表单使用数组中的正确值填充,当我单击提交时,将显示正确的值。 The problem I have is the slide toggles and check-boxes don't VISUALLY update themselves based on the boolean (1/0) values in the array when an entry is selected from the drop down.我遇到的问题是当从下拉列表中选择一个条目时,滑动开关和复选框不会根据数组中的布尔 (1/0) 值在视觉上更新自己。

Even though the form values submitted are correct, the DISPLAY value is wrong not representing the back-end data correctly.即使提交的表单值正确, DISPLAY值也是错误的,不能正确表示后端数据。 How do you VISUALLY update the slide toggles and check-boxes so they respond to the array object true/false value?您如何以视觉方式更新幻灯片切换和复选框,以便它们响应数组对象的真/假值?

Do I need to use getElementById to update these controls since the form has already been rendered?由于表单已经呈现,我是否需要使用 getElementById 来更新这些控件?

The setValue initializes the text boxes visually, but I can't get the slide toggles to visually show their state correctly. setValue 以视觉方式初始化文本框,但我无法通过滑动切换来正确地直观地显示它们的状态。 As I said before, everything shows their values correctly when I submit the form.正如我之前所说,当我提交表单时,一切都会正确显示它们的值。

Angular HTML有角度的 HTML

   <form [formGroup]="collectionUpdateForm" (ngSubmit)="updateCollectionData(collectionUpdateForm.value)">
      <!--Again as in ts file. For small forms this is fine, but you can make these dynamically with ngFor or something-->
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Select Language</mat-label>
         <mat-select formControlName="languageID" [(ngModel)]="languageSelected">
            <mat-option *ngFor="let language of languages" [value]="language.languageID">
               {{language.languageName}}
            </mat-option>
         </mat-select>
      </mat-form-field>
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Select Category</mat-label>
         <mat-select formControlName="categoryID" [(ngModel)]="categorySelected" (click)="getCollections()">
            <mat-option *ngFor="let category of categories" [value]="category.categoryID">
               {{category.categoryName}}
            </mat-option>
         </mat-select>
      </mat-form-field>
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Collection Name</mat-label>
         <input matInput formControlName="collectionName">
      </mat-form-field>
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Event Name</mat-label>
         <input matInput formControlName="eventName">
      </mat-form-field>
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Collection Description</mat-label>
         <textarea matInput formControlName="collectionDesc" cdkTextareaAutosize></textarea>
      </mat-form-field>
      <mat-form-field class="full-width" appearance="outline">
         <mat-label>Product ID</mat-label>
         <input matInput formControlName="productID">
      </mat-form-field>
      <mat-slide-toggle formControlName="onlineOnly" [(ngModel)]="onlineOnlyChecked" (input)="onlineOnlyChecked">Online Only</mat-slide-toggle>
      <mat-slide-toggle formControlName="display" [(ngModel)]="displayChecked" (input)="displayChecked">Display</mat-slide-toggle>
      <button mat-raised-button color="primary">Update</button>
   </form>

Angular Typescript角度打字稿

   collectionUpdateForm = new FormGroup
   (
      {
         languageID: new FormControl(),
         categoryID: new FormControl(),
         collectionID: new FormControl(),
         collectionName: new FormControl(),
         eventName: new FormControl(),
         collectionDesc: new FormControl(),
         productID: new FormControl(),
         onlineOnly: new FormControl(),
         display: new FormControl(),
      }
   )

   getCollectionSelected()
   {
      //alert( "Language = " + this.selectedCollection?.languageID )
      //alert( "Channel = " + this.channelSelected )
      //alert( "Category = " + this.categorySelected )
      this.collectionUpdateForm.setValue( this.selectedCollection as any )
      //alert( "Online Only = " + this.selectedCollection?.onlineOnly )
      //this.onlineOnlyChecked = false;
      //this.displayChecked = false;

      //this.channelID = this.selectedCollection?.channelID;
      //this.languageID = this.selectedCollection?.languageID;
      //this.collectionID = this.selectedCollection?.collectionID;
   }

Angular Service角度服务

export interface MediaCollectionInfoInterface
{
   collectionID: number;
   collectionName: string;
   eventName: string;
   categoryID: number;
   productID: string;
   languageID: string;
   onlineOnly: number;
   collectionDesc: string;
   display: number;
}

After trial and error, I found the following solution:经过反复试验,我找到了以下解决方案:

<mat-slide-toggle formControlName="onlineOnly" [checked]="onlineOnlyChecked" >Online Only <mat-slide-toggle formControlName="display" [checked]="displayChecked" >Display <mat-slide-toggle formControlName="onlineOnly" [checked]="onlineOnlyChecked" >仅在线 <mat-slide-toggle formControlName="display" [checked]="displayChecked" >Display

Both onlineOnlyChecked and displayChecked are boolean variables that get their values assigned from the array object. onlineOnlyChecked 和 displayChecked 都是布尔变量,它们的值是从数组对象中分配的。

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

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