Javascript 从api获取选项时如何在angular js中选择选项
我有一个应用程序,连接了选择框,如州、市。 现在我必须从控制器输入州名和城市名。 我找不到路Javascript 从api获取选项时如何在angular js中选择选项,javascript,angularjs,Javascript,Angularjs,我有一个应用程序,连接了选择框,如州、市。 现在我必须从控制器输入州名和城市名。 我找不到路 <label>State</label> <select ng-change="cities(state);" class="form-control" ng-model="state" required> <option selected>Select</option>
<label>State</label>
<select ng-change="cities(state);" class="form-control" ng-model="state" required>
<option selected>Select</option>
<option ng-repeat="x in state" value="{{x.State}}">{{x.State}}
</option>
</select>
<label>City</label>
<select class="form-control" ng-model="city" ng-change="location(city);" required>
<option selected>Select</option>
<option ng-repeat="y in cities" value="{{y.city}}">{{y.city}}
</option>
</select>
提前感谢。为ng repeat数组分配不同的名称:
$scope.states=response.data.State;
$scope.cities=response.date.Cities;
<label>State</label>
<select ng-change="cities(state);" class="form-control" ng-model="state" required>
<option selected>Select</option>
<option ng-repeat="x in states" value="{{x.State}}">{{x.State}}
</option>
</select>
<label>City</label>
<select class="form-control" ng-model="city" ng-change="location(city);" required>
<option selected>Select</option>
<option ng-repeat="y in cities" value="{{y.city}}">{{y.city}}
</option>
</select>
需要使用不同名称的select ng型号:
$scope.states=response.data.State;
$scope.cities=response.date.Cities;
<label>State</label>
<select ng-change="cities(state);" class="form-control" ng-model="state" required>
<option selected>Select</option>
<option ng-repeat="x in states" value="{{x.State}}">{{x.State}}
</option>
</select>
<label>City</label>
<select class="form-control" ng-model="city" ng-change="location(city);" required>
<option selected>Select</option>
<option ng-repeat="y in cities" value="{{y.city}}">{{y.city}}
</option>
</select>
状态
挑选
{{x.State}}
城市
挑选
{{y.city}
此外,还可以通过使用ng选项来实现
让控制器:
app.controller('MainCtrl', function($scope) {
$scope.States = [
{"Name":"Value 1","ID":1},
{"Name":"Value 2","ID":2},
{"Name":"Value 3","ID":3},
{"Name":"Value 4","ID":4}
];
$scope.Cities = [
{"Name":"Value 1","ID":1},
{"Name":"Value 2","ID":2},
{"Name":"Value 3","ID":3},
{"Name":"Value 4","ID":4}
];
$scope.selectedState=2; //setting default select
$scope.selectedCity=4;
});
HTML
所选状态为:{{selectedState}}
所选城市为:{{selectedCity}
工作打捞器
有关选择下拉列表的不同方式,请参见plunker您将使用
状态
作为ngModel
以及ngRepeat