Javascript 如何使用selected from some select item作为参数从视图中调用函数?
我怎样才能做到这一点Javascript 如何使用selected from some select item作为参数从视图中调用函数?,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,我怎样才能做到这一点 <select> <option ng-repeat="item in items" value="item">{{item.name}}</option> </select> <a ng-click="foo(item)">Action</a> 我理解,项在元素范围中定义。但我需要将当前选择的项目发送到foofoonction。我如何做到这一点?不要使用ng repeat,而是使用ng optio
<select>
<option ng-repeat="item in items" value="item">{{item.name}}</option>
</select>
<a ng-click="foo(item)">Action</a>
我理解,
项
在元素
范围中定义。但我需要将当前选择的项目发送到foo
foonction。我如何做到这一点?不要使用ng repeat
,而是使用ng options
:
<select ng-model="selectedItem" ng-options="item as item.name for item in items" >
</select>
然后,您应该能够使用以下功能:
<a ng-click="foo(selectedItem)">Action</a>
那么,@SharikovVladislav,这个答案有用吗?可能有用。我只有明天才能尝试。我举了两个例子。和。你的代码真的工作得很好。但是为什么呢?我的怎么了?在我的代码中,ng selected设置了$scope.selectedItem,但它不起作用。为什么?我问这个问题,因为我已经在代码中尝试了额外的$scope
变量,但它对我没有帮助。ng selected
是一个属性,用于
元素,而不是
。选择了ng
,您可以将某个选项设置为“已选择”。它不会将所选内容保存到范围变量。
<a ng-click="foo(selectedItem)">Action</a>