Angularjs select 对象的 ng-模型绑定
Angularjs select ng-model binding for object
我想要一个 select 对象数组。example 但是不知何故,我无法访问 selected 对象的属性。
js---
$scope.test1={};
$scope.test = [{'name':'test1'},{'name':'test2'},{'name':'test3'}];
html--
<select style="width:100px;height:25px;" ng-model="test1">
<option ng-repeat="attribute in test" value="{{attribute}}">{{attribute['name']}}</option>
</select>
{{test1}}
{{test1.name}}
此处,test1.name空白。
这是因为 select
value
被解释为字符串。不作为对象。当然,字符串没有 name
属性。如果您希望您的值包含整个对象,您可以使用 ng-options
。 Read the documentation here.
使用ngOpions this way.It gives proper controll than ng-repeat
<select style="width:100px;height:25px;" ng-model="test1"
ng-options="attribute.name for attribute in test">
这是Plunker
我想要一个 select 对象数组。example 但是不知何故,我无法访问 selected 对象的属性。
js---
$scope.test1={};
$scope.test = [{'name':'test1'},{'name':'test2'},{'name':'test3'}];
html--
<select style="width:100px;height:25px;" ng-model="test1">
<option ng-repeat="attribute in test" value="{{attribute}}">{{attribute['name']}}</option>
</select>
{{test1}}
{{test1.name}}
此处,test1.name空白。
这是因为 select
value
被解释为字符串。不作为对象。当然,字符串没有 name
属性。如果您希望您的值包含整个对象,您可以使用 ng-options
。 Read the documentation here.
使用ngOpions this way.It gives proper controll than ng-repeat
<select style="width:100px;height:25px;" ng-model="test1"
ng-options="attribute.name for attribute in test">
这是Plunker