基于angular實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的生日插件
寫了一個(gè)生日聯(lián)動(dòng)插件具體的效果是這樣的:
具體的數(shù)據(jù)
我取得數(shù)據(jù)是今年的數(shù)據(jù),如果是想要做三級(jí)聯(lián)動(dòng)的日期插件,改一下時(shí)間就好了
var app=angular.module("dataPicker",[])
app.factory('dataPicker', ['$http', '$q', function ($http, $q) {
return {
query: function () {
var lengthYear=100;
var dataPicker={
month:[],
year:[],
day:[]
};
var data = new Date();
var nowyear = data.getFullYear();
for(var i=nowyear,j=0; i>nowyear-lengthYear;i--,j++){
dataPicker.year[j]=i;
}
for(var i=0;i<=11;i++){
if(i<9){
dataPicker.month[i]='0'+(i+1);
}else{
dataPicker.month[i]=String(i+1);
}
}
return dataPicker;
}
}
}])
directive插件的主要內(nèi)容
app.directive('selectDatepicker', function ($http,dataPicker) {
return {
restrict: 'EAMC',
replace: false,
scope: {
birthday: '=birthday'
},
transclude: true,
template: '<span>生日</span>'+
'<select class="sel_year" ng-model="birY" ng-change="changeYear()"><option ng-repeat="x in yearAll">{{x}}</option></select>'+
'<select class="sel_month" ng-model="birM" ng-change="changeMonth()" ng-disabled="birY==\'\'"><option ng-repeat="x in MonthAll">{{x}}</option> </select>'+
'<select class="sel_day" ng-model="birD" ng-disabled="birM==\'\'" ng-change="changeDay()"><option ng-repeat="x in DayAll">{{x}}</option></select>',
link: function (scope, element){
var arr=[];
scope.birthday=scope.birthday=='0000-00-00'?"":scope.birthday
var shuju=dataPicker.query()
scope.yearAll=shuju.year;
scope.MonthAll=shuju.month;
if(scope.birthday){
scope.birY=scope.birthday.birthday.split('-')[0];
scope.birM=String(scope.birthday.birthday.split('-')[1])
}else{
scope.birY="";
scope.birM="";
}
scope.getDaysInOneMonth=function(year, month){
var month1 = Number(month);
month1=parseInt(month1,10)
var d= new Date(Number(year),month1,0);
return d.getDate();
}
scope.getDayArr=function(day){
shuju.day=[];
for(var i=0; i<day;i++){
if(i<9){
shuju.day[i]='0'+(i+1)
}else{
shuju.day[i]=String((i+1));
}
}
}
if(scope.birthday){
var day=scope.getDaysInOneMonth(scope.birthday.birthday.split('-')[0],scope.birthday.birthday.split('-')[1]);
scope.getDayArr(day)
scope.DayAll=shuju.day;
scope.birD=scope.birthday.birthday.split('-')[2]
}
scope.changeYear=function(){
scope.birD="";
scope.birM="";
}
scope.changeMonth=function(){
var day=scope.getDaysInOneMonth(scope.birY,scope.birM);
console.log(day)
scope.getDayArr(day);
scope.DayAll=shuju.day;
scope.birD="";
}
scope.changeDay=function(){
scope.returnDate();
}
scope.returnDate=function(){
if(!scope.birD||!scope.birY||!scope.birM){
scope.birthday.returnValue="";
}else{
arr[0]=scope.birY;
arr[1]=scope.birM;
arr[2]=scope.birD;
scope.birthday.returnValue=arr.join("-");
}
}
}
}
})
});
html
<div select-datepicker birthday="birthday">
js 傳入的數(shù)據(jù)
$scope.birthday={
birthday:1993-01-20,
returnValue:'',
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能示例
- Angularjs實(shí)現(xiàn)下拉框聯(lián)動(dòng)的示例代碼
- Angularjs自定義指令實(shí)現(xiàn)三級(jí)聯(lián)動(dòng) 選擇地理位置
- 基于AngularJs + Bootstrap + AngularStrap相結(jié)合實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)代碼
- 實(shí)例詳解AngularJS實(shí)現(xiàn)無限級(jí)聯(lián)動(dòng)菜單
- AngularJS實(shí)現(xiàn)的省市二級(jí)聯(lián)動(dòng)功能示例【可對(duì)選項(xiàng)實(shí)現(xiàn)增刪】
相關(guān)文章
angular實(shí)現(xiàn)表單驗(yàn)證及提交功能
這篇文章主要為大家詳細(xì)介紹了angular實(shí)現(xiàn)表單驗(yàn)證及提交功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
AngularJS頁面訪問時(shí)出現(xiàn)頁面閃爍問題的解決
這篇文章主要介紹了AngularJS框架使用中出現(xiàn)頁面閃爍問題的解決方法,閃爍問題一般是初始化未加載完畢造成的,需要的朋友可以參考下2016-03-03
Angular之toDoList的實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Angular之toDoList的實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
詳解Angular5/Angular6項(xiàng)目如何添加熱更新(HMR)功能
這篇文章主要介紹了詳解Angular5/Angular6項(xiàng)目如何添加熱更新(HMR)功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Angular開發(fā)實(shí)踐之服務(wù)端渲染
這篇文章主要介紹了Angular開發(fā)實(shí)踐之服務(wù)端渲染,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Angular發(fā)布1.5正式版,專注于向Angular 2的過渡
Angular團(tuán)隊(duì)最近發(fā)布了Angular 1.5的正式版,該版本實(shí)現(xiàn)了一次重大的升級(jí),它讓仍在使用1.X版本的開發(fā)者將能夠更容易地過渡到Angular 2的開發(fā)2016-02-02
淺談AngularJS中$http服務(wù)的簡(jiǎn)單用法
這篇文章主要介紹了淺談AngularJS中$http服務(wù)的簡(jiǎn)單用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
詳解封裝基礎(chǔ)的angular4的request請(qǐng)求方法
這篇文章主要介紹了詳解封裝基礎(chǔ)的angular4的request請(qǐng)求方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

