这篇文章将为大家详细讲解有关Angularjs自定义指令如何实现三级联动选择地理位置,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站建设、成都网站制作、班戈网络推广、小程序定制开发、班戈网络营销、班戈企业策划、班戈品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供班戈建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
Angularjs自定义指令实现三级联动效果
代码
<html lang="zh-CN" ng-app="myApp"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="jquery.min.js"></script> <script src="angular.js"></script> <script src="bootstrap.min.js"></script> <link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" > <style type="text/css"> select { width : 116px; } .selectLocation select { display: block; float: left; margin-bottom: 2px; } </style> <script type="text/javascript"> var myApp = angular.module('myApp', []); myApp.controller('Ctrl', ['$scope', 'utilsService', function($scope, utilsService){ $scope.location = ''; $scope.$watch('location', function(newValue) { console.log(newValue) console.log(utilsService.isEmptyObj(newValue)) }) // if (isEmptyObj($scope.location)) { // //error // } }]); myApp.factory("utilsService", function() { return { isEmptyObj : function(obj) { var flag = true; for(var i in obj) { if (obj[i] != '') { flag = false; break; } } return flag; } } }) myApp.directive("custLocation", ['$http', function($http) { return { restrict: 'A', scope: { ngModel : '=' }, templateUrl: 'tmpl.html', link: function(scope, elem, attrs) { scope.country = ''; scope.province = ''; scope.city = ''; scope.detailAddress = ''; $http.get("location.json").success(function(data) { scope.countryList = data.country; }); scope.$watch('detailAddress', function(newValue) { // console.log(scope.country.name + scope.province.name + scope.city + newValue) scope.ngModel = { "country" : scope.country == null || scope.country == '' ? '' : scope.country.name, "province" : scope.province == null || scope.province == '' ? '' : scope.province.name, "city" : scope.city || '', "detailAddress" : newValue }; }); scope.changeCountry = function() { if (scope.country == null) { scope.country = ''; scope.province = ''; scope.city = ''; scope.detailAddress = ''; scope.ngModel = ''; } else { scope.ngModel = { "country" : scope.country.name, "province" : scope.province == null || scope.province == '' ? '' : scope.province.name, "city" : scope.city || '', "detailAddress" : scope.detailAddress }; } } scope.changeProvince = function () { scope.ngModel = { "country" : scope.country.name, "province" : scope.province == null || scope.province == '' ? '' : scope.province.name, "city" : scope.city || '', "detailAddress" : scope.detailAddress }; } scope.changeCity = function() { scope.ngModel = { "country" : scope.country.name, "province" : scope.province == null || scope.province == '' ? '' : scope.province.name, "city" : scope.city || '', "detailAddress" : scope.detailAddress }; } } }; }]); </script> </head> <body ng-controller="Ctrl"> <div cust-location ng-model="location"></div> </body> </html>
tmpl.html
<div class="selectLocation"> <div> <select class="btn btn-info btn-sm" ng-change="changeCountry()" ng-model="country" ng-options="C.name for C in countryList"> <option value="">国家</option> </select> </div> <div> <select class="btn btn-info btn-sm" ng-change="changeProvince()" ng-model="province" ng-options="p.name for p in country.province"> <option value="">省份/直轄市</option> </select> </div> <div> <select class="btn btn-info btn-sm" ng-change="changeCity()" ng-model="city" ng-options="c for c in province.city"> <option value="">市</option> </select> </div> <div > <input type="text" class="form-control" ng-model="detailAddress" placeholder="详细地址" ng-disabled="country=='' || country==null" /> </div> </div>
关于“Angularjs自定义指令如何实现三级联动选择地理位置”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
分享题目:Angularjs自定义指令如何实现三级联动选择地理位置
本文链接:https://www.cdcxhl.com/article48/jjpiep.html
成都网站建设公司_创新互联,为您提供关键词优化、做网站、营销型网站建设、响应式网站、企业网站制作、品牌网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联