NG-OPTIONS中的硬编码值与NG-REPEAT的动态值

时间:2022-11-25 17:29:07

Let's say we have this data and it's being returned by an API

假设我们有这个数据它被一个API返回

$scope.arrayOfStudentObjects = [
    { name : 'Ashley', gender : 'female' },
    { name : 'Tom', gender : 'male' },
    { name : 'Scott', gender : 'male' }
];

Then the values that we put in < select > are in a scope

然后我们在< select >中放入的值就在范围内

$scope.gender = [ { type : 'male' }, { type : 'female' } ];

Now we want to display the values and correctly set the selection for the gender in the select dropdown

现在我们想要显示值,并正确地设置select下拉菜单中性别的选择。

<tr ng-repeat="student in arrayOfStudentObjects track by $index">
    <select ng-model="student.gender" ng-options="sex.type for sex in gender"></select>
</tr>

However, I'm not sure why it's not displaying/selecting the proper gender for that student. Please see Plunker. http://plnkr.co/edit/o0Wt2Qg8BFXmeeMqFBIE?p=preview

然而,我不确定为什么它不显示/选择适当的性别给那个学生。请参阅砰砰作响。http://plnkr.co/edit/o0Wt2Qg8BFXmeeMqFBIE?p=preview

1 个解决方案

#1


3  

change your <select...> to

改变你的 <选择…> ,

 <select ng-model="student.gender" ng-options="sex.type as sex.type for sex in gender"></select>

otherwise the value it compares to is the full object.

否则,它比较的值就是完整的对象。

forked plunk:

分叉的砰砰作响:

http://plnkr.co/edit/azBhpIEUkybXmQWliMOR?p=preview

http://plnkr.co/edit/azBhpIEUkybXmQWliMOR?p=preview

#1


3  

change your <select...> to

改变你的 <选择…> ,

 <select ng-model="student.gender" ng-options="sex.type as sex.type for sex in gender"></select>

otherwise the value it compares to is the full object.

否则,它比较的值就是完整的对象。

forked plunk:

分叉的砰砰作响:

http://plnkr.co/edit/azBhpIEUkybXmQWliMOR?p=preview

http://plnkr.co/edit/azBhpIEUkybXmQWliMOR?p=preview