AngularJS ng-options 最初从一个选择值
AngulaJS ng-options initially selected value from an
我有一个带有 属性 的对象:值结构应该用于 ng-options 以创建 select 下拉列表。我还有一个 ng-model 变量,其中包含 属性 当前应该 selected。问题是我不知道如何修复初始 selection。
你可以在这里找到代码
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars track by id">
不使用"track by"
Do not use as and track by in the same expression. They are not
designed to work together.
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars ">
</select>
演示版
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.cars = {
frd : "Ford",
ft1 : "Fiat",
vlv : "Volvo"
}
$scope.selectedCar = 'ft1';
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Select a car:</p>
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars ">
</select>
<h1>You selected model: {{selectedCar}}</h1>
</div>
<p>This example demonstrates the use of an object as the data source when creating a dropdown list.</p>
</body>
</html>
我有一个带有 属性 的对象:值结构应该用于 ng-options 以创建 select 下拉列表。我还有一个 ng-model 变量,其中包含 属性 当前应该 selected。问题是我不知道如何修复初始 selection。 你可以在这里找到代码
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars track by id">
不使用"track by"
Do not use as and track by in the same expression. They are not designed to work together.
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars ">
</select>
演示版
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.cars = {
frd : "Ford",
ft1 : "Fiat",
vlv : "Volvo"
}
$scope.selectedCar = 'ft1';
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>Select a car:</p>
<select ng-model="selectedCar" ng-options="id as value for (id, value) in cars ">
</select>
<h1>You selected model: {{selectedCar}}</h1>
</div>
<p>This example demonstrates the use of an object as the data source when creating a dropdown list.</p>
</body>
</html>