Gar*_*uuk 7 angularjs ng-options
我在我的资源对象中有一个方法:
resources.type
otherstuff: 'more strings'
type:'specifictype'
morestuff: 'morestuff'
Run Code Online (Sandbox Code Playgroud)
用户可以通过下拉列表/通过另一个调用来更改此类型,该调用获取所有可能类型的列表,这些类型看起来像resourceList.types,其中包含像json这样的对象列表
types:
[
{name:'resourcetype1'},
{name:'resourcetype2'},
etc...
],
Run Code Online (Sandbox Code Playgroud)
我的HTML看起来像:
<select ng-model="resources.type" ng-options="name.name for name in resourceList.types">
</select>
Run Code Online (Sandbox Code Playgroud)
选择/下拉框填充了我的resourceList.type内容,但是当页面加载时,ng-model没有设置为已选择的资源类型.实际上,当您单击时,它会在下拉列表的顶部选择一个空白条目.这个角度有点挑剔吗?如何让ng-model以我想要的方式运行?
我尝试使用不同的方法来获取重复,但我觉得这更像是角度连接模型的方式.它是否只是将字符串与ng-options列表匹配?
这是plnkr,因为你可以看到它不是默认为type1
在Angular中,模型是真理的唯一来源.
这意味着如果您想要选择(并绑定到您的ngModel)值,您需要将其分配给模型:
<select ng-model="resources.type"
ng-options="type.name as type.name for type in resourceList.types">
</select>
$scope.resources = {...};
$scope.resourceList = {
...
types: [
{name: 'resourcetype1'},
{name: 'resourcetype2'},
...
]
};
// Set the model to the desired value
$scope.resources.type = $scope.resourceList.types[0].name;
Run Code Online (Sandbox Code Playgroud)
另见这个简短的演示.
| 归档时间: |
|
| 查看次数: |
15507 次 |
| 最近记录: |