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)文章
AngularJS監(jiān)聽(tīng)路由變化的方法
本篇文章主要介紹了AngularJS監(jiān)聽(tīng)路由變化的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
AngularJS基礎(chǔ) ng-submit 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-submit 指令,這里對(duì)ng-submit 指令的基礎(chǔ)資料做了詳細(xì)介紹整理,并附有代碼示例,有需要的小伙伴可以參考下2016-08-08
詳解基于Bootstrap+angular的一個(gè)豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個(gè)豆瓣電影app ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06
angularjs 獲取默認(rèn)選中的單選按鈕的value方法
下面小編就為大家分享一篇angularjs 獲取默認(rèn)選中的單選按鈕的value方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
AngularJS實(shí)現(xiàn)的省市二級(jí)聯(lián)動(dòng)功能示例【可對(duì)選項(xiàng)實(shí)現(xiàn)增刪】
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的省市二級(jí)聯(lián)動(dòng)功能,涉及事件監(jiān)聽(tīng)、響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,此外還具備對(duì)選項(xiàng)進(jìn)行增刪的功能,需要的朋友可以參考下2017-10-10
AngularJS directive返回對(duì)象屬性詳解
這篇文章主要為大家纖細(xì)介紹了AngularJS directive返回對(duì)象屬性的相關(guān)內(nèi)容,感興趣的小伙伴們可以參考一下2016-03-03
利用VS Code開(kāi)發(fā)你的第一個(gè)AngularJS 2應(yīng)用程序
這篇文章主要給大家介紹了關(guān)于利用VS Code如何開(kāi)發(fā)你的第一個(gè)AngularJS 2應(yīng)用程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來(lái)一起看看吧。2017-12-12
Angular2使用Augury來(lái)調(diào)試Angular2程序
這篇文章主要介紹了Angular2使用Augury來(lái)調(diào)試Angular2程序,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Angular2中如何使用ngx-translate進(jìn)行國(guó)際化
本篇文章主要介紹了Angular2中使用ngx-translate進(jìn)行國(guó)際化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

