简体   繁体   English

将当前选定的字段传递给按钮的javascript调用

[英]Pass currently selected field to a button's javascript call

I have some select fields populated from a database as well as some buttons to go with them. 我有一些从数据库填充的选择字段以及一些与之配套的按钮。

<td class="col-md-1" colspan="1" style="text-align: center; vertical-align: middle;">
    <button class="btn btn-primary" data-ng-click="removeContract(name.value, contractIndex)">button</button>
</td>
<td class="col-md-5" colspan="5">
    <label class="control-label">{{item.fields[132].displayName}}</label>
    <select size="5">
        <option data-ng-repeat="name in entities[0].itemAttrs">{{name.value}}</option>
    </select> 
</td>  

The select is populated by the database using ng-repeat. 该选择由数据库使用ng-repeat填充。 When the button is pressed removeContract() is run and I want to pass the currently selected option in the select field. 当按下按钮时,运行removeContract(),我想在选择字段中传递当前选择的选项。

I tried passing name.value but because the button is outside the ng-repeat it doesn't understand it. 我尝试传递name.value但是因为按钮在ng-repeat之外,所以无法理解。 How do I fix this? 我该如何解决?

Put a model on the select element that's a property of your controller (it's not strictly necessary to include a dot , but it's a good practice to avoid confusing scope scenarios), and pass that to your function: 将模型放在控制器属性的select元素上(并不一定要包含点 ,但最好避免混淆范围方案),然后将其传递给函数:

<td ...>
    <button data-ng-click="removeContract(ctrl.selectValue, contractIndex)">button</button>
    <!-------------------------------------------^ -->
</td>

<td class="col-md-5" colspan="5">
    <label ...>{{item.fields[132].displayName}}</label>
    <select size="5" ng-model="ctrl.selectValue">
    <!--------------------------------^ -->
        <option data-ng-repeat="name in entities[0].itemAttrs">{{name.value}}</option>
    </select>
</td>

https://docs.angularjs.org/api/ng/directive/select https://docs.angularjs.org/api/ng/directive/select

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

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