简体   繁体   English

Angular-material,如何绑定 object 数组

[英]Angular-material, how to bind an object array

I'm dealing with this code which was written by another developer, and I'm not used to angular.我正在处理由另一个开发人员编写的这段代码,我不习惯 angular。 I'm trying to bind the data.order.order_items to an object in the component, to do an additional logic in the component side when the user clicks the button.我正在尝试将 data.order.order_items 绑定到组件中的 object,以便在用户单击按钮时在组件端执行附加逻辑。

<!-- some code -->

    <form class="form">
      <h4 class="sku-list-title">SKU List</h4>
      <mat-slide-toggle
        [checked]="isChecked"
        (change)="isChecked = $event.source.checked"
        class="toggle"
        >Edit SKUs</mat-slide-toggle
      >
      <div
        class="item-container"
        *ngFor="let element of data.order.order_items"
        [(ngModel)]="orderItems"
      >
        <mat-form-field>
          <mat-label>SKU</mat-label>
          <input
            [value]="element.item.seller_sku"
            matInput
            tabindex="-1"
            [disabled]="true"
          />
        </mat-form-field>
        <mat-form-field>
          <mat-label>New SKU</mat-label>
          <input
            [placeholder]="element.item.new_seller_sku"
            matInput
            tabindex="-1"
            [disabled]="!isChecked"
          />
        </mat-form-field>
        <mat-form-field>
          <mat-label>Quantity</mat-label>
          <input
            matInput
            maxlength="5"
            [value]="element.quantity"
            tabindex="-1"
            [disabled]="!isChecked"
          />
        </mat-form-field>
      </div>
    </form>
  </div>

<!-- some code -->
<div mat-dialog-actions class="actions full-width">
  <button mat-flat-button color="warn" (click)="onNoClick()">Cancel</button>
  <button
    mat-flat-button
    color="primary"
    (click)="onClick()"
    [mat-dialog-close]="closeAnswer"
  >
    Accept
  </button>
</div>

Component side组件端

@Component({
  selector: "app-message-dialog",
  templateUrl: "./message-dialog.component.html",
  styleUrls: ["./message-dialog.component.scss"],
})
export class MessageDialogComponent implements OnInit {
  orderItems: any; //This object would bind the order_items
  //some code

 onClick() {
  //some code
  this.orderItems //doesn't get the binded data.

How can I bind the data from the data.order.order_items that is updated in that input, to the object this.orderItems?如何将该输入中更新的 data.order.order_items 中的数据绑定到 object this.orderItems? I tried with the ngModel, but I guess i'm missing something or doing it in the wrong element.我尝试使用 ngModel,但我想我遗漏了一些东西或在错误的元素中做这件事。

Thanks in advance!提前致谢!

you are binded the data but forgot to display data you can use following syntax {{element }}您已绑定数据但忘记显示数据您可以使用以下语法 {{element }}

you should use the ngModel inside input tag to get the value from corresponding input tag or another option is to use formControl to get the value您应该使用输入标签内的 ngModel 从相应的输入标签中获取值,或者另一个选项是使用 formControl 来获取值

  1. NG Model <input matInput maxlength="5" [value]="element.quantity" tabindex="-1" [disabled]="!isChecked" [(ngModel)]="orderItems" /> NG Model <input matInput maxlength="5" [value]="element.quantity" tabindex="-1" [disabled]="!isChecked" [(ngModel)]="orderItems" />

  2. Form Control表单控件

visit this site -> https://angular.io/api/forms/FormControl访问这个网站-> https://angular.io/api/forms/FormControl

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

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