AngularJS實(shí)現(xiàn)多級(jí)下拉框
本文實(shí)例為大家分享了AngularJS實(shí)現(xiàn)多級(jí)下拉框的具體代碼,供大家參考,具體內(nèi)容如下
<div ng-app="MultiDropDownApp" ng-controller="MultiDropDownControl as vm"> ? ? <label>選擇地址:</label> ? ? <!--ng-options加載所有選擇項(xiàng),ng-model記錄當(dāng)前選擇項(xiàng)--> ? ? <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="">請(qǐng)選擇</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="">請(qǐng)選擇</option> ? ? </select> </div>
<script src="~/Scripts/angular.min.js"></script> <script> ? ? var app = angular.module('MultiDropDownApp', []); ? ? //可以添加上自己注入的服務(wù) ? ? app.controller('MultiDropDownControl', ['$scope', '$http', ? ? ? ? function ($scope, $http) { ? ? ? ? ? ? var vm = this; ? ? ? ? ? ? vm.provinceSort = []; ? ? ? ? ? ? vm.citySort = []; ? ? ? ? ? ? //選擇省級(jí)單位,初始化市級(jí)數(shù)據(jù) ? 二級(jí)聯(lián)動(dòng) ? ? ? ? ? ? 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) { ? ? ? ? ? ? ? ? ? ? // 請(qǐng)求失敗執(zhí)行代碼 ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? } ? ? ? ? ? ? //初始化頁(yè)面 ? ? ? ? ? ? function init() { ? ? ? ? ? ? ? ? //省 ? ? ? ? ? ? ? ? $http({ ? ? ? ? ? ? ? ? ? ? method: 'POST', ? ? ? ? ? ? ? ? ? ? url: '/AngularjsStudy/GetProvinceSort', ? ? ? ? ? ? ? ? ? ? data: {} ? ? ? ? ? ? ? ? }).then(function successCallback(data) { ? ? ? ? ? ? ? ? ? ? //加載下拉框數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? vm.provinceSort = data.data; ? ? ? ? ? ? ? ? ? ? //設(shè)置默認(rèn)選項(xiàng) ? ? ? ? ? ? ? ? ? ? vm.province = vm.provinceSort[0]; ? ? ? ? ? ? ? ? }, function errorCallback(response) { ? ? ? ? ? ? ? ? ? ? // 請(qǐng)求失敗執(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 = "長(zhǎng)沙市" }); ? ? ? ? ? ? ? ? ? ? districts.Add(new District() { id = 5, fatherID = 1, name = "岳陽(yáng)市" }); ? ? ? ? ? ? ? ? ? ? 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 = "綿陽(yáng)市" }); ? ? ? ? ? ? ? ? ? ? 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é)點(diǎn)FatherID=0 ? ? /// </summary> ? ? public int fatherID { get; set; } ? ? public string name { get; set; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular中使用響應(yīng)式表單的詳細(xì)步驟
Angular提供了兩種處理表單的方式模板驅(qū)動(dòng)表單和響應(yīng)式表單(也稱為模型驅(qū)動(dòng)表單),使用模板驅(qū)動(dòng)表單時(shí),模板指令被用來(lái)構(gòu)建表單的內(nèi)部表示,在本文中,您探討了如何將響應(yīng)式表單應(yīng)用于一個(gè)示例 Angular 應(yīng)用程序2024-02-02angular6根據(jù)environments配置文件更改開發(fā)所需要的環(huán)境的方法
這篇文章主要介紹了angular6根據(jù)environments配置文件更改開發(fā)所需要的環(huán)境的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法
這篇文章主要介紹了Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12詳解使用angular-cli發(fā)布i18n多國(guó)語(yǔ)言Angular應(yīng)用
這篇文章主要介紹了詳解使用angular-cli發(fā)布i18n多國(guó)語(yǔ)言Angular應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05AngularJS ui-router (嵌套路由)實(shí)例
本篇文章主要介紹了AngularJS ui-router (嵌套路由)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Angular6實(shí)現(xiàn)拖拽功能指令drag實(shí)例詳解
這篇文章主要為大家介紹了Angular6實(shí)現(xiàn)拖拽功能指令drag實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11AngularJS基礎(chǔ) ng-click 指令示例代碼
本文介紹AngularJS ng-click 指令,這里整理了ng-click指令的基礎(chǔ)知識(shí)并且附有簡(jiǎn)單示例代碼和實(shí)現(xiàn)效果圖,有需要的小伙伴參考下2016-08-08Angularjs 滾動(dòng)加載更多數(shù)據(jù)
AngularJS 通過(guò)新的屬性和表達(dá)式擴(kuò)展了 HTML。本文主要給大家介紹Angularjs 滾動(dòng)加載更多數(shù)據(jù)的相關(guān)知識(shí),需要的朋友參考下吧2016-03-03