您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“AngularJS怎么實現(xiàn)多級下拉框”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“AngularJS怎么實現(xiàn)多級下拉框”吧!
具體代碼如下
<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 = []; //選擇省級單位,初始化市級數(shù)據(jù) 二級聯(lián)動 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) { // 請求失敗執(zhí)行代碼 }); } //初始化頁面 function init() { //省 $http({ method: 'POST', url: '/AngularjsStudy/GetProvinceSort', data: {} }).then(function successCallback(data) { //加載下拉框數(shù)據(jù) vm.provinceSort = data.data; //設置默認選項 vm.province = vm.provinceSort[0]; }, function errorCallback(response) { // 請求失敗執(zhí)行代碼 }); } //初始化 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> /// 根節(jié)點FatherID=0 /// </summary> public int fatherID { get; set; } public string name { get; set; } }
到此,相信大家對“AngularJS怎么實現(xiàn)多級下拉框”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。