本篇内容主要讲解“angularjs怎么实现多级下拉框”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“AngularJS怎么实现多级下拉框”吧!具体代码如下<div ng-app=
本篇内容主要讲解“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; }}
到此,相信大家对“AngularJS怎么实现多级下拉框”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: AngularJS怎么实现多级下拉框
本文链接: https://lsjlt.com/news/325731.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0