Javascript 将元素的属性传递给角度为4/5的函数
我想通过单击按钮将元素的属性值传递给函数,下面是我的操作方法Javascript 将元素的属性传递给角度为4/5的函数,javascript,angular,angular5,Javascript,Angular,Angular5,我想通过单击按钮将元素的属性值传递给函数,下面是我的操作方法 {{option}} 提交 您可以在change()上将变量设置为所选选项的值和项目ID,并在单击按钮时传入这些变量 <div> <ul #list> <li class="radio" *ngFor="let option of options; let j = index" id={{i}}-{{j}} #item> <label><inpu
-
{{option}}
提交
您可以在change()
上将变量设置为所选选项的值和项目ID,并在单击按钮时传入这些变量
<div>
<ul #list>
<li class="radio" *ngFor="let option of options; let j = index" id={{i}}-{{j}} #item>
<label><input type="radio" #opt name={{i}} value={{option}} (change)="selectedOptionValue=option.value; selectedItemID=item.id" /> {{option}} </label>
</li>
</ul>
</div>
<button type="button" (click)='func(selectedOptionValue, selectedItemID, list)'>submit</button>
-
{{option}}
提交
只需确保在后端
func
函数中检查这些值,查看它们是否已设置。模板引用变量opt
仅在ngFor
循环中可见。我们可以理解原因:由于在options
中可以有多个项目,因此可以有多个单选输入元素,因此不清楚按钮单击事件处理程序中会引用哪个opt
。同样的问题也适用于变量项
,但是变量列表
应该可以工作,因为它与按钮在同一范围内。是的,做了类似的事情