Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

angular - PrimeNG checkbox ngModel with object

I want to implement the functionality shown below,I want to use ngFor

enter image description here

environment:

[email protected]
[email protected]

html code:

<p-table [columns]="cols" [value]="layers">
  <ng-template pTemplate="body" let-layer let-columns="columns">
    <tr>
      <ng-container *ngFor="let col of columns">
        <td *ngIf="col.field == 'name'">
          <span>{{layer.name}}</span>
        </td>
        <td *ngIf="col.field != 'name'" style="text-align:center">
          <p-checkbox [name]="col.field" [(ngModel)]="layer[col.field]"></p-checkbox>
          <!-- <input type="checkbox" name="" value="" [(ngModel)]="layer[col.field]"> -->
        </td>
      </ng-container>
    </tr>
  </ng-template>
</p-table>

ts code:

cols = [
  {label:'name',field:'name'},
  {label:'a',field:'a'},
  {label:'b',field:'b'},
  {label:'c',field:'c'},
  {label:'d',field:'d'},
]

layers = [
  {
    name:'x1',
    id:'1'
    a:true,
    b:true,
    c:false,
    d:false
  },
  {
    name:'x2',
    id:'2'
    a:false,
    b:true,
    c:false,
    d:true
  },
  {...}
]

error:

ERROR Error: Uncaught (in promise): TypeError: this.model.indexOf is not a function

when I not use checkbox of primeNG, just use <input type="checkbox" name="" value="" [(ngModel)]="layer[col.field]">,it working well.

how can I fix it? Please!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...