AngularJS实现多级下拉框
更新时间:2022年03月25日 16:44:39 作者:Lulus
这篇文章主要为大家详细介绍了AngularJS实现多级下拉框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了AngularJS实现多级下拉框的具体代码,供大家参考,具体内容如下
<div ng-app="MultiDropDownApp" ng-controller="MultiDropDownControl as vm"> <label>选择地址:</label> <!--ng-options加载所有选择项,ng-model记录当前选择项--> <select ng-model="vm.province" ng-options="x.name for x in vm.provinceSort" ng-change="vm.selectProvince()" id="" value="" class="form-control width-25"> <option value="">请选择</option> </select> <label>—</label> <select ng-model="vm.city" ng-options="x.name for x in vm.citySort" id="" value="" class="form-control width-25"> <option value="">请选择</option> </select> </div>
<script src="~/Scripts/angular.min.js"></script>
<script>
var app = angular.module('MultiDropDownApp', []);
//可以添加上自己注入的服务
app.controller('MultiDropDownControl', ['$scope', '$http',
function ($scope, $http) {
var vm = this;
vm.provinceSort = [];
vm.citySort = [];
//选择省级单位,初始化市级数据 二级联动
vm.selectProvince = function () {
var fatherID = vm.province.id;
vm.citySort = [];
$http({
method: 'POST',
url: '/AngularjsStudy/GetChildrenSort',
data: { fatherID: fatherID }
}).then(function successCallback(data) {
vm.citySort = data.data;
}, function errorCallback(response) {
// 请求失败执行代码
});
}
//初始化页面
function init() {
//省
$http({
method: 'POST',
url: '/AngularjsStudy/GetProvinceSort',
data: {}
}).then(function successCallback(data) {
//加载下拉框数据
vm.provinceSort = data.data;
//设置默认选项
vm.province = vm.provinceSort[0];
}, function errorCallback(response) {
// 请求失败执行代码
});
}
//初始化
init();
}])
</script>Controller
public ActionResult GetProvinceSort()
{
List<District> districts = new List<District>();
districts.Add(new District() {id=1,fatherID=0,name="湖南省" });
districts.Add(new District() { id =2, fatherID = 0, name = "湖北省" });
districts.Add(new District() { id =3, fatherID = 0, name = "四川省" });
return Json(districts);
}
public ActionResult GetChildrenSort(int fatherID)
{
List<District> districts = new List<District>();
switch (fatherID)
{
case 1:
districts.Add(new District() { id = 4, fatherID = 1, name = "长沙市" });
districts.Add(new District() { id = 5, fatherID = 1, name = "岳阳市" });
districts.Add(new District() { id = 6, fatherID = 1, name = "株洲市" });
return Json(districts);
case 2:
districts.Add(new District() { id = 7, fatherID = 2, name = "武汉市" });
districts.Add(new District() { id = 8, fatherID = 2, name = "宜昌市" });
return Json(districts);
case 3:
districts.Add(new District() { id = 9, fatherID = 3, name = "成都市" });
districts.Add(new District() { id = 10, fatherID = 3, name = "遂宁市" });
districts.Add(new District() { id = 11, fatherID = 3, name = "巴中市" });
districts.Add(new District() { id = 12, fatherID = 3, name = "绵阳市" });
districts.Add(new District() { id = 13, fatherID = 3, name = "南充市" });
return Json(districts);
default:
districts.Add(new District() { id = 14, fatherID = -1, name = "不知道你选了什么∑q|゚Д゚|p" });
return Json(districts);
}
}Model
public class District
{
public int id { get; set; }
/// <summary>
/// 根节点FatherID=0
/// </summary>
public int fatherID { get; set; }
public string name { get; set; }
}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
详解基于Bootstrap+angular的一个豆瓣电影app
本篇文章主要介绍了基于Bootstrap+angular的一个豆瓣电影app ,非常具有实用价值,需要的朋友可以参考下2017-06-06
AngularJS实现的省市二级联动功能示例【可对选项实现增删】
这篇文章主要介绍了AngularJS实现的省市二级联动功能,涉及事件监听、响应及页面元素动态操作相关技巧,此外还具备对选项进行增删的功能,需要的朋友可以参考下2017-10-10
利用VS Code开发你的第一个AngularJS 2应用程序
这篇文章主要给大家介绍了关于利用VS Code如何开发你的第一个AngularJS 2应用程序的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面来一起看看吧。2017-12-12
Angular2中如何使用ngx-translate进行国际化
本篇文章主要介绍了Angular2中使用ngx-translate进行国际化,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-05-05


最新评论