2016-10-01 81 views
0

在2角我有这个component.html角2 * ngIf为 '本'

<li *ngFor="let something of somethings"> 
    <span>{{something}}</span> 
    <input class="doit" type="text" *ngIf="iscalled" /> 
<div id="container"> 
    <button class="btn btn-warning editsection (click)="editAction()">Edit</button> 
</div> 
</li> 

与此component.ts

editAction(){ this.iscalled = true; } 

是,默认情况下,在我的组件中设置为false。

基本上,对于每个somethingsomethings我产生,连同我的列表是分配给它的输入字段和运行editAction()的按钮。只有当用户点击editAction()按钮时,该按钮才会出现。

现在,照样,点击editAction()按钮将启用列表中的所有输入字段。我想限制它的意思是确切的li元素。

我不知道Angular 2是否有针对此问题的特定操作,或者这是否为纯javascript解决方案。

+1

您是否考虑过使用ngFor公开的索引? – jonrsharpe

回答

1

注意:此设置不设置默认值iscalled

<li *ngFor="let something of somethings"> 
    <span>{{something}}</span> 
    <input class="doit" type="text" 
      *ngIf="something.iscalled" />  //<<<===changed 

    <div id="container"> 
      <button class="btn btn-warning 
      editsection 
      (click)="editAction(something)">  //<<<===changed 
       Edit 
      </button> 
    </div> 
</li> 

editAction(something){ something.iscalled = true; } 

,如果你想切换它,那么你可以做以下,

editAction(something){ something.iscalled != something.iscalled; } 
+0

非常感谢!但是,切换选项似乎不起作用。我知道你的错字,所以它不像我复制粘贴它。逻辑结束了什么也不做。 – abrahamlinkedin

+1

它应该工作。我很确定。如果您为'something'对象使用任何模型,而不是确保向该模型添加iscalled属性。它将按预期工作。 – micronyks

+1

我明白了。如果它不起作用,你可以把'something.iscalled!= something.iscalled'改成'something.iscalled =!something.iscalled' – micronyks