AngularJS實現(xiàn)多級下拉框
更新時間:2022年03月25日 16:44:39 作者:Lulus
這篇文章主要為大家詳細介紹了AngularJS實現(xiàn)多級下拉框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了AngularJS實現(xiàn)多級下拉框的具體代碼,供大家參考,具體內(nèi)容如下
<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', []);
? ? //可以添加上自己注入的服務(wù)
? ? 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;
? ? ? ? ? ? ? ? ? ? //設(shè)置默認選項
? ? ? ? ? ? ? ? ? ? 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; }
}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJS基礎(chǔ) ng-submit 指令簡單示例
本文主要介紹AngularJS ng-submit 指令,這里對ng-submit 指令的基礎(chǔ)資料做了詳細介紹整理,并附有代碼示例,有需要的小伙伴可以參考下2016-08-08
詳解基于Bootstrap+angular的一個豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個豆瓣電影app ,非常具有實用價值,需要的朋友可以參考下2017-06-06
AngularJS實現(xiàn)的省市二級聯(lián)動功能示例【可對選項實現(xiàn)增刪】
這篇文章主要介紹了AngularJS實現(xiàn)的省市二級聯(lián)動功能,涉及事件監(jiān)聽、響應(yīng)及頁面元素動態(tài)操作相關(guān)技巧,此外還具備對選項進行增刪的功能,需要的朋友可以參考下2017-10-10
利用VS Code開發(fā)你的第一個AngularJS 2應(yīng)用程序
這篇文章主要給大家介紹了關(guān)于利用VS Code如何開發(fā)你的第一個AngularJS 2應(yīng)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧。2017-12-12
Angular2使用Augury來調(diào)試Angular2程序
這篇文章主要介紹了Angular2使用Augury來調(diào)試Angular2程序,非常具有實用價值,需要的朋友可以參考下2017-05-05
Angular2中如何使用ngx-translate進行國際化
本篇文章主要介紹了Angular2中使用ngx-translate進行國際化,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

