「selected」値のselectタグを作成したい。私はこれをやっています ng-repeat カスタムディレクティブをに追加できないため ng-options。これがコードです。
<select class="ng-valid ng-dirty" ng-model="selectedCity">
<option ng-repeat="city in allCities" value="{{city.id}}" after-render-cities>{{city.name}}</option>
</select>
これにより、値=?string:1?のオプションが追加されます。よく検索しました。以来 ng-options この問題は解決しますが、ディレクティブを追加する必要があります アフターレンダリング都市 オプションごとに。この問題を解決するにはどうすればよいですか?
回答:
回答№1は0これを使って
<select class="ng-valid ng-dirty" ng-model="selectedCity" ng-options="city.id as city.name for city in allCities">
</select>
最初の値を選択するには、コントローラーに次のコードを記述します
$scope.selectedCity = $scope.allCities[0].id;
回答№2の場合は0
値の解決にng-initを使用=?string:1?
<select class="ng-valid ng-dirty" ng-model="selectedCity" ng-init="selectedCity=allCities[0].id">
<option ng-repeat="city in allCities" value="{{city.id}}" after-render-cities>{{city.name}}</option>
</select>
ng-selectedを使用できる別の選択肢 フィドル
<option ng-repeat="(key,city) in allCities" ng-selected="key==0" value="{{city.id}}" after-render-cities>{{city.name}}</option>
</select>
回答№3の場合は0
このコードを試してください
Javaスクリプト
var app = angular.module("myApp", []);
app.controller("SampleController", function ($scope) {
$scope.Cities = [
{ Id: 1, Name: "New York" },
{ Id: 2, Name: "Colombo" },
{ Id: 3, Name: "Real Madrid" },
{ Id: 4, Name: "Bostan" },
{ Id: 5, Name: "Birmingham" }
];
$scope.City= 3;
});
HTML
<select ng-options="C.Id as C.Name for C in Cities" ng-model="City"></select>
このブログを読んでください、それはすべてが説明されています。
回答№4の場合は0
カスタムを作成することができます directive
、using select
そして ng-options
.
スニペット:
JS:
template: "<select ng-options="item.id as item.name for item in list" ng-model="data"></select>",
HTML:
<div select-option list="list" ng-model="data"></div>
デモを参照してください ここに.