2017-07-27 98 views
0

我想获得从列表项所有的“ID”,“检查”我怎样才能把一个数值在幻灯片的toogle角材料

<div *ngFor="let item of list"> 
    <md-slide-toggle [(ngModel)]="item.id"> 
     item.name 
    </md-slide-toggle> 
</div> 
+0

请问您的对象有任何'boolean'财产?或者,如何知道切换是否被选中或取消选中?如果可能的话,添加一个'list'数组的例子,或者它的一个对象。 – Nehal

+0

布尔是这种情况下必须像条件 –

+0

你是什么意思的“布尔是这种情况下必须像条件”? – Nehal

回答

1

您可以创建一个单独的数组,保留哪个项目的轨迹在'列表'中已被标记为true。然后,您可以使用change输出event向组件发出有关item的信息并对其执行操作。

示例代码:

HTML:

<div *ngFor="let item of list; let i = index"> 
    <md-slide-toggle [checked]="flagArray[i]" 
        (change)="sendToServer($event, i, item)"> 
     {{item.name}} 
    </md-slide-toggle> 
</div> 

component.ts:

flagArray = []; 

    list = [ 
    { id: '1', name: 'item 1'}, 
    { id: '2', name: 'item 2'}, 
    { id: '3', name: 'item 3'}, 
    { id: '4', name: 'item 4'}, 
    ] 

    constructor(){ 
    for(let i=0; i<this.list.length; i++){ 
     this.flagArray.push(false); 
    } 
    } 

    sendToServer(event, index, item){ 
    this.flagArray[index] = event.checked; 

    if(event.checked == true){ 
     // add code to send item to server 
     alert("item to send to server: " + JSON.stringify(item));   
    } 
    else{ 
     // add code to remove item from server 
     alert("item to remove from server: " + JSON.stringify(item)); 
    } 
    } 

Plunker demo

相关问题