2016-05-21 4 views
1

、私は複数のドロップダウンボックスが必要です。すべてのデータは私のAPIのjsonからのものです。私の問題は、私はAPIからデータを設定する方法を知っていますが、ここで私は3つの異なるURLを持っています。選択した国に応じてドロップダウンを行います。 (url:blabla/countries)、次に2番目のドロップダウンで選択した国(url:blabla/countries/countryId/states)と3番目のドロップダウンで州を設定する必要がある選択された州(url:blabla/countries/countryId/states/statesId/city)。 選択した国または州のIDを次のドロップダウンボックスのURLに追加する方法がわかりません。ウェブアプリケーションにドロップダウンの角度を掛ける

$http.get('url') 
 
       .success(function(response) 
 
     {$scope.countryes=response;}); 
 
<div> 
 
     Country: 
 
     <select id="country" ng-model="country" ng-options="country.id as country.name for country in countryes"> 
 
      <option value=''>{{countryes.name}}</option> 
 
     </select> 
 
    </div> 
 
    <div> 
 
     States <select id="city" ng-disabled="!country" ng-model="suburbs" ng-options="state as state.name for state in states"><option value='state'>Select</option></select> 
 
    </div> 
 
    <div> 
 
     City <select id="suburb" ng-disabled="!suburbs" ng-model="suburb" ng-options="suburb for suburb in suburbs"><option value=''>Select</option></select>   
 
    </div>

のthnx郡と状態のため、誰も

答えて

1

使用ng-changeにコントローラー広告の新機能

$scope.getState = function(){ 
    //this function run when you select country 
    $http.get('getStateUrl',{country: $scope.country}) 

} 


$scope.getCity= function(){ 
    //this function run when you select state 
    $http.get('getCityUrl',{state: $scope.suburbs}) 
} 

<div> 
    Country: 
    <select id="country" ng-model="country" ng-options="country.id as country.name for country in countryes" ng-change='getState()'> 
    <option value=''>{{countryes.name}}</option> 
    </select> 
</div> 
<div> 
    States 
    <select id="city" ng-disabled="!country" ng-model="suburbs" ng-options="state as state.name for state in states" ng-change='getCity()'> 
    <option value='state'>Select</option> 
    </select> 
</div> 
<div> 

を選択10

+0

thnxですが、どこのIDが設定されていますか? – Arter

+0

これはng resourceとng-changeで行います。そして、最初のドロップダウンで私はすべての国のリストを持っています。今問題は、次のドロップダウンのIDを設定する方法ですか? Url Myapiurl/country /:countryID/statesのようになります。私はcountryid、選択された国のidに設定する必要があります。私はリソースにadd(countryid:@ country.id)を試みますが、動作しません。誰かが私にidを設定する方法を説明することはできますか?別のAppで私はこれを行うが、URLの最後の位置にIDがあった。どのようにidを設定する必要があるときに変更するURLの真ん中に? Thnx – Arter

+0

モデル国IDがng-model = 'country'の場合、 '$ scope.country'はcontry idです – aseferov

0

最初のthnxから@aseferovへのヘルプ。 将来誰かが同じ質問をした場合、これは私のコードです、どうすればいいですか?

app.controller('myCtrl', ['$scope', '$routeParams', 'narudzbaResourceFactory', 'narudzbaResourceStatesFactory', 'narudzbaResourceStatesCityFactory', 
function ($scope, routeParams, narudzbaResourceFactory, narudzbaResourceStatesFactory, narudzbaResourceStatesCityFactory) { 

    narudzbaResourceFactory.query(function (res) { 
     $scope.countries = res; 
    }); 
    $scope.getState = function() { 

     $scope.states = narudzbaResourceStatesFactory.query({country: $scope.country.id}); 

    }; 

    $scope.getCity = function() { 
     $scope.cities = narudzbaResourceStatesCityFactory.query({state: $scope.state.id}); 

    }; 

を(私は、3つの異なるドロップダウンダウンの前に、ドロップからのIDに依存している)これは私のCTRLであり、これは私の工場

app.factory('narudzbaResourceFactory', ['$resource', 
function ($resource) { 
    var countries = $resource('/locationlist/countries', { 
     query: {method: 'GET', isArray: true 
     } 
    }); 
    return countries; 
}]); 

app.factory('narudzbaResourceStatesFactory', ['$resource', 
function ($resource) { 
    var states = $resource('locationlist/countries/:country/states', {country: '@country'}, { 
     query: {method: 'GET', isArray: true 
     } 
    }); 
    return states; 
}]); 

app.factory('narudzbaResourceStatesCityFactory', ['$resource', 
function ($resource) { 
    var cities = $resource('/locationlist/states/:state/cities', 
      {state: '@state'}, { 
     query: {method: 'GET', isArray: true 
     } 
    }); 
    return cities; 
}]); 

され、ビューに私はこの

ようにそれを使用します
<div> 
    Country: 
    <select id="country" ng-model="country" ng-options="country as country.name for country in countries track by country.id" ng-change="getState()"> 
     <option value='{{country.id}}'>{{country.name}}</option> 
    </select> 
</div> 
<div> 
    States <select id="state" ng-disabled="!country" ng-model="state" ng-options="state as state.name for state in states track by state.id" ng-change="getCity()"> 
     <option value='{{state.id}}'>{{state.name}}</option> 
    </select> 
</div> 
<div> 
    City <select id="city" ng-disabled="!state" ng-model="city" ng-options="city as city.name for city in cities track by city.id"> 
     <option value='{{city.id}}'>{{city.name}}</option></select>   
</div> 
関連する問題