我從JSON(國家,城市)創建依賴關係下拉菜單。根據國家選擇,城市將在第二個下拉列表中填充。如果我得到國家的價值它返回對象與城市列表。AngularJs - 從ng-model(ng-option)獲取價值
請等待從github raw得到國家下拉值。
var app = angular.module('myApp',[])
app.controller('dropdown', function($scope, $http){
\t $scope.istrue = true;
\t $scope.userInfo = [];
\t $http.get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json').then(function(response){
\t \t $scope.countriesToCities = response.data;
\t \t //console.log($scope.countriesToCities)
\t },function(response){
\t \t console.log(response.status);
\t });
\t
\t $scope.populateCities = function() {
\t \t $scope.istrue = false;
\t \t //console.log($scope);
\t \t $scope.cityArray = $scope.users.country;
\t }
\t
\t $scope.selectedCity = function(){
\t \t console.log($scope.city)
\t }
\t
\t $scope.pushInArray = function() {
\t \t var user = angular.copy($scope.users);
\t \t $scope.userInfo.push(user);
\t }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="dropdown">
\t <input type="text" name="name" ng-model="users.name" placeholder="Name">
\t <input type="text" name="email" ng-model="users.email" placeholder="Email">
\t <input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number">
\t <select ng-model="users.country" ng-change='populateCities()' ng-options="x for (x, y) in countriesToCities"></select>
\t <select ng-hide="istrue" ng-model="users.city" ng-change='selectedCity()' ng-options="x for x in cityArray"></select>
\t <button ng-click="pushInArray()">Add</button>
\t <pre>{{userInfo}}</pre>
</div>
如何獲得國家的價值?
試試這個'NG選項= 「country.countryId作爲country.countryName爲國家countriesToCities」'。當然,我不知道什麼是國會對象的關鍵。 –
這裏的國家名稱作爲關鍵字 – vinox
你想把'countryName'放在'users.country'或'countryId'在'users.country'中嗎? –