2017-07-26 81 views
0

我有一個連接了狀態,城市等選擇框的應用程序。 現在我必須從控制器提供州名和城市名稱。 我無法找到方法。如何從api中提取選項時選擇角度js中的選項

<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> 

我在控制器中試過這段代碼,但不起作用。

 $scope.state=response.data.State; 
    $scope.city=response.date.Cities; 

在此先感謝。

+0

您是使用'state'爲'ngModel'和以及與'ngRepeat' – Satpal

回答

0

分配NG重複陣列不同的名稱:

$scope.states=response.data.State; 
$scope.cities=response.date.Cities; 

必須不同名的選擇NG-型號:

<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> 
+0

感謝這項工作。 –

1

而且,它可以通過使用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

<body ng-controller="MainCtrl"> 
    <select ng-options="value.ID as value.Name for value in States" 
    ng-model="selectedState" ></select> 
    Selected state is : {{selectedState}} 
    <hr> 
    <select ng-options="value.ID as value.Name for value in Cities" 
    ng-model="selectedCity" ></select> 
    Selected city is : {{selectedCity}} 
    </body> 

工作Plunker​​3730580

對於選擇下拉不同的方式看plunker click here

相關問題