我有一组数据items
:
$scope.items = [
{
"value": 1,
"text": "1st"
},
{
"value": 2,
"text": "2nd"
}
]
我希望能够创建一个选择标签,例如:
<select ng-model="currentSelection" ng-options="item.value as item.text for item in items"></select>
这是显示所有items
的下拉列表的正常方法。
是否可以将"item for item in items"
分配为范围变量并将该范围变量分配给ng-options
?IE(在控制器中):
$scope.options = "item.value as item.text for item in items";
和视图:
<select ng-model="currentSelection" ng-options="{{options}}"></select>
现在,我对最后一个示例的实现会导致选项加载为一个空字符串,我能想到的唯一原因是,在首先加载ng属性/变量之后,正在加载范围变量。
<</p>看一下:
jsfiddle
var app =angular.module('app',[]);
app.controller('Ctrl', function($scope){
$scope.currentSelection = "";
$scope.expression = "item.value as item.text for item in items";
$scope.items = [
{
"value": 1,
"text": "1st"
},
{
"value": 2,
"text": "2nd"
}
]
});
这应该起作用。另一种方法是创建指令。