简体   繁体   English

AngularJS-在嵌套的ng-repeat中访问dropdown的值

[英]Angularjs - Access the value of dropdown in a nested ng-repeat

I'm can't access the value of the select in the controller when the select is inside an ng-repeat. 当选择位于ng-repeat内时,我无法在控制器中访问选择的值。

Here is the sample of Html (Everything display properly): 这是Html的示例(一切显示正确):

<tr ng-repeat="reservationItem in dataList">
    <select ng-model="detail" ng-change= "haha($index)" ng-options="n for n in [1,2,3,42]" >
        <option value="Choose"> Choose </options>
    </select> 
</tr>

`` ``

In the controller, inside the function haha() I try to get the value of the selected dropdown: $scope.haha = function(index){ console.log(index); console.log($scope.detail); } 在控制器的haha()函数内部,我尝试获取所选下拉列表的值: $scope.haha = function(index){ console.log(index); console.log($scope.detail); } $scope.haha = function(index){ console.log(index); console.log($scope.detail); }

The first console.log always show 0 The second console.log always shows undefined 第一个console.log始终显示0,第二个console.log始终显示未定义

Can someone tell me how to access the value of a select inside a nested from a controller. 有人可以告诉我如何访问嵌套在控制器中的select的值。 ? Thanks 谢谢

In this case, you have multiple detail ng-model. 在这种情况下,您将具有多个详细信息ng模型。 This confuses the angular, look at ng-model like an ID. 这会混淆角度的ng模型,就像ID。 you can't have duplicate. 您不能重复。 You would need to pull the select out, or attached detail to reservationItem ie. 您需要将选择内容拉出,或将详细信息附加到reserveItem上。 reservationItem.detail ReservationItem.detail

Also, you won't need ng-change as angular autobinds that value to the model. 同样,您也不需要ng-change,因为该值会自动将角度绑定到模型。

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

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