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)文章
angular6根據(jù)environments配置文件更改開發(fā)所需要的環(huán)境的方法
這篇文章主要介紹了angular6根據(jù)environments配置文件更改開發(fā)所需要的環(huán)境的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
詳解使用angular-cli發(fā)布i18n多國語言Angular應用
這篇文章主要介紹了詳解使用angular-cli發(fā)布i18n多國語言Angular應用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Angular6實現(xiàn)拖拽功能指令drag實例詳解
這篇文章主要為大家介紹了Angular6實現(xiàn)拖拽功能指令drag實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
AngularJS基礎(chǔ) ng-click 指令示例代碼
本文介紹AngularJS ng-click 指令,這里整理了ng-click指令的基礎(chǔ)知識并且附有簡單示例代碼和實現(xiàn)效果圖,有需要的小伙伴參考下2016-08-08

