我在这里看到了Angular选择指令的文档:http://docs.angularjs.org/api/ng.directive:select。 我不知道如何设置默认值。这很令人困惑。
选择作为数组中值的标签
这里是对象。
{
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
}
的html(工作)。
<select><option ng-repeat="value in prop.values">{{value}}</option></select>
然后我试图在选择元素中添加一个ng-option属性,将prop.value
设置为默认选项(不工作)。
ng-options="(prop.value) for v in prop.values"
我做错了什么?
所以假设那个对象在你的范围内。
<div ng-controller="MyCtrl">
<select ng-model="prop.value" ng-options="v for v in prop.values">
</select>
</div>
function MyCtrl($scope) {
$scope.prop = {
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
};
}
Work Plunkr: http://plnkr.co/edit/wTRXZYEPrZJRizEltQ2g
选择的angular文档*并没有明确回答这个问题,但它是存在的。 如果你看一下script.js
,你会看到这个。
function MyCntrl($scope) {
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark'},
{name:'yellow', shade:'light'}
];
$scope.color = $scope.colors[2]; // Default the color to red
}
这是html。
<select ng-model="color" ng-options="c.name for c in colors"></select>
这似乎是在<select>
上用ng-options
默认选择值的一个比较明显的方法。 如果你有不同的标签/值,它也能工作。
*
这来自Angular 1.2.7