您好,登錄后才能下訂單哦!
本篇內容主要講解“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怎么實現多級下拉框”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。