AngularJS 下拉列表问题
Issue with AngularJS Dropdown
我已经创建了 AngularJS 个可靠的下拉菜单,它可以很好地处理脚本文件中的静态数据。现在如何将数据从 URL 绑定到这些下拉列表。我没有单独的 url 用于下拉菜单,有一个 url 可以提供所有内容。
我应该如何解析数据以获得所需的输出?我对此很陌生,如果有人能提供帮助,将不胜感激!!
这里第一个工作的 Punker:
http://plnkr.co/edit/okKOeQViflRseqrOYeOY?p=preview
问题是您的问题陈述了一些内容,而您的演示显示了其他内容。
您可以使用 angular 直接动态解析 JSON 数据,然后您没有 ng-change
功能来检测选择了哪个下拉列表。请参阅下面的代码,了解我如何解析 html
本身的数据。
<div class="col-sm-4">
<am-multiselect class="input-lg"
template-url="multiselect.tmpl.html"
ng-model="selectedcountry" ms-header="Select country" style="width:200px;"
options="c.CountryName for c in table"
ng-change="setState(selectedcountry)"
change="selected()"></am-multiselect>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedState.length}} State(s) selected"
ng-model="selectedState" ms-header="Select States"
options="s.STATE for s in table"
ng-change="setCity(selectedState)"
template-url="multiselect.tmpl.html" style="width:500px;"
change="selected()">
</am-multiselect>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedCity.length}} City(ies) selected"
ng-model="selectedCity" ms-header="Select City"
options="m.CityName for m in table"
template-url="multiselect.tmpl.html" style="width:500px;"
change="selected()"></am-multiselect>
</div>
</div>
接下来您的第二个下拉菜单 STATE 不会触发更改,即使范围已更改。
所以使用 $watch
查看范围的变化,当它发生时将其应用到 CITY
所以你的 js:
angular.module('app', ['am.multiselect']);
angular.module('app')
.controller('ctrl', function($scope, $http, $location, $filter,$window) {
var tableData = {
"Table": [{
"CountryUid": 3,
"CountryName": "INDIA",
"STATE": "AndraPradesh",
"CityId": 3,
"CityName": "Vijayawada"
}, {
"CountryUid": 2,
"CountryName": "USA",
"STATE": "Florida",
"CityId": 3,
"CityName": "Tampa"
}, {
"CountryUid": 3,
"CountryName": "INDIA",
"STATE": "Assam",
"CityId": 3,
"CityName": "Jorhat"
}]
};
var countries = [];
var states = [];
var cities = [];
$scope.table = tableData.Table;
$scope.countries = countries;
$scope.states = states;
$scope.cities = cities;
$scope.selectedsite = null;
$scope.selectedState = [];
$scope.selectedCity = [];
$scope.setState = function(country) {
$scope.selectedStates = tableData.Table.filter(function(el) {
return el.CountryName === country
});
$scope.selectedState = $scope.selectedStates.map(function(elm) {return elm.STATE;});
}
$scope.setCity = function(state) {
selectedCity = tableData.Table.filter(function(el) {
return el.STATE === state
});
$scope.selectedCity = selectedCity.map(function(elm) {return elm.CityName;});
}
$scope.$watch('selectedStates', function(newVal, oldVal, theScope) {
$scope.selectedCity = newVal.map(function(elm) {return elm.CityName;});
console.log(newVal,$scope.selectedCity);
});
PS: 上面的插件不是很完美你可能会遇到一些小问题,你应该可以解决。
我已经创建了 AngularJS 个可靠的下拉菜单,它可以很好地处理脚本文件中的静态数据。现在如何将数据从 URL 绑定到这些下拉列表。我没有单独的 url 用于下拉菜单,有一个 url 可以提供所有内容。
我应该如何解析数据以获得所需的输出?我对此很陌生,如果有人能提供帮助,将不胜感激!!
这里第一个工作的 Punker:
http://plnkr.co/edit/okKOeQViflRseqrOYeOY?p=preview
问题是您的问题陈述了一些内容,而您的演示显示了其他内容。
您可以使用 angular 直接动态解析 JSON 数据,然后您没有 ng-change
功能来检测选择了哪个下拉列表。请参阅下面的代码,了解我如何解析 html
本身的数据。
<div class="col-sm-4">
<am-multiselect class="input-lg"
template-url="multiselect.tmpl.html"
ng-model="selectedcountry" ms-header="Select country" style="width:200px;"
options="c.CountryName for c in table"
ng-change="setState(selectedcountry)"
change="selected()"></am-multiselect>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedState.length}} State(s) selected"
ng-model="selectedState" ms-header="Select States"
options="s.STATE for s in table"
ng-change="setCity(selectedState)"
template-url="multiselect.tmpl.html" style="width:500px;"
change="selected()">
</am-multiselect>
</div>
</div>
<div class="form-group">
<div class="col-sm-4">
<am-multiselect class="input-lg" multiple="true" ms-selected="{{selectedCity.length}} City(ies) selected"
ng-model="selectedCity" ms-header="Select City"
options="m.CityName for m in table"
template-url="multiselect.tmpl.html" style="width:500px;"
change="selected()"></am-multiselect>
</div>
</div>
接下来您的第二个下拉菜单 STATE 不会触发更改,即使范围已更改。
所以使用 $watch
查看范围的变化,当它发生时将其应用到 CITY
所以你的 js:
angular.module('app', ['am.multiselect']);
angular.module('app')
.controller('ctrl', function($scope, $http, $location, $filter,$window) {
var tableData = {
"Table": [{
"CountryUid": 3,
"CountryName": "INDIA",
"STATE": "AndraPradesh",
"CityId": 3,
"CityName": "Vijayawada"
}, {
"CountryUid": 2,
"CountryName": "USA",
"STATE": "Florida",
"CityId": 3,
"CityName": "Tampa"
}, {
"CountryUid": 3,
"CountryName": "INDIA",
"STATE": "Assam",
"CityId": 3,
"CityName": "Jorhat"
}]
};
var countries = [];
var states = [];
var cities = [];
$scope.table = tableData.Table;
$scope.countries = countries;
$scope.states = states;
$scope.cities = cities;
$scope.selectedsite = null;
$scope.selectedState = [];
$scope.selectedCity = [];
$scope.setState = function(country) {
$scope.selectedStates = tableData.Table.filter(function(el) {
return el.CountryName === country
});
$scope.selectedState = $scope.selectedStates.map(function(elm) {return elm.STATE;});
}
$scope.setCity = function(state) {
selectedCity = tableData.Table.filter(function(el) {
return el.STATE === state
});
$scope.selectedCity = selectedCity.map(function(elm) {return elm.CityName;});
}
$scope.$watch('selectedStates', function(newVal, oldVal, theScope) {
$scope.selectedCity = newVal.map(function(elm) {return elm.CityName;});
console.log(newVal,$scope.selectedCity);
});
PS: 上面的插件不是很完美你可能会遇到一些小问题,你应该可以解决。