Angularjs 手寫日歷的實(shí)現(xiàn)代碼(不用插件)
本文介紹了Angularjs 手寫日歷的實(shí)現(xiàn)代碼(不用插件),分享給大家,具體如下:
效果:
Html:
<div class="plan_content_box" data-ng-init="showTime()"> <div class="field" style="width: 100%;"> <span class="field_label" style="width: 100%;text-align: center;"> <select id="time_year" ng-change="change_year(select_year)" ng-model="select_year" ng-options="x.id as x.value for x in all_year"> <!--<option value="1900">1900</option>--> </select> 年 <select id="time_month" ng-change="change_month(select_month)" ng-model="select_month" ng-options="x.id as x.value for x in all_month"> </select> 月 {{active_day}} 日 </span> </div> <table class="table table-bordered hover_td" style="border: none;"> <tr id="float_td"> <td>星期日</td> <td>星期一</td> <td>星期二</td> <td>星期三</td> <td>星期四</td> <td>星期五</td> <td>星期六</td> <td ng-repeat="day in days track by $index" ng-click="change_day(day)" ng-class="{true:'active',false:''}[day==active_day]" ng-model="day">{{day}}</td> </tr> </table> </div>
js:
// 創(chuàng)建日歷 $scope.all_year = []; $scope.all_month = []; $scope.showTime = function() { //在select中填入年份 for(var year = 2016; year < 2050; year++) { var obj_1 = {'value': year, 'id': year} $scope.all_year.push(obj_1); } //在select中填入月份 for(var month = 1; month < 13; month++) { var obj_2 = {'value': month, 'id': month} $scope.all_month.push(obj_2); } console.log($scope.all_year) //初始化顯示 當(dāng)前年和月 $scope.show_now() } //當(dāng)select的選中的option發(fā)送變化的觸發(fā)的事件 $scope.change_year = function(data) { $scope.showDays(data, $scope.select_month) } $scope.change_month = function(data) { $scope.showDays($scope.select_year, data) } //返回指定的月份的天數(shù) 月份1-12 $scope.calDays = function (year, month) { return new Date(year, month, 0).getDate(); } $scope.days = []; //展示指定的年和月的所有日期 $scope.showDays = function(year, month) { $scope.days = []; //得到表示指定年和月的1日的那個(gè)時(shí)間對(duì)象 var date = new Date(year, month - 1, 1); //1.先添加響應(yīng)的空白的li:這個(gè)月1號(hào)是星期幾,就添加幾個(gè)空白的li var dayOfWeek = date.getDay(); //得到1日是星期幾 for(var i = 0; i < dayOfWeek; i++) { $scope.days.push(""); } //計(jì)算一個(gè)月有多少天 var daysOfMonth = $scope.calDays(year, month); //2. 從1號(hào)開始添加li for(var i = 1; i <= daysOfMonth; i++) { $scope.days.push(i) } } $scope.active_day = '' $scope.select_year = '' $scope.select_month = '' //初始化顯示 當(dāng)前年和月 $scope.show_now = function() { var now = new Date(); // $("#time_year").val(now.getFullYear()); // $("#time_month").val(now.getMonth() + 1); $scope.active_day = now.getDate(); $scope.select_year = now.getFullYear(); $scope.select_month = now.getMonth() + 1; $scope.showDays($scope.select_year, $scope.select_month) } $scope.change_day = function(day){ $scope.active_day = "" $scope.active_day = day } // 以上是創(chuàng)建日歷
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
angularjs中使用ng-bind-html和ng-include的實(shí)例
下面小編就為大家?guī)?lái)一篇angularjs中使用ng-bind-html和ng-include的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04自定義Angular指令與jQuery實(shí)現(xiàn)的Bootstrap風(fēng)格數(shù)據(jù)雙向綁定的單選與多選下拉框
這篇文章主要介紹了自定義Angular指令與jQuery實(shí)現(xiàn)的Bootstrap風(fēng)格數(shù)據(jù)雙向綁定的單選與多選下拉框 的相關(guān)資料,需要的朋友可以參考下2015-12-12AngularJS學(xué)習(xí)筆記之ng-options指令
ng-options是angular-1.3新出的一個(gè)指令,這篇文章就來(lái)介紹這個(gè)指令的用法.有需要的小伙伴可以參考下。2015-06-06angularJs使用ng-repeat遍歷后選中某一個(gè)的方法
今天小編就為大家分享一篇angularJs使用ng-repeat遍歷后選中某一個(gè)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Angularjs 雙向綁定時(shí)字符串的轉(zhuǎn)換成數(shù)字類型的問(wèn)題
這篇文章主要介紹了Angular js雙向綁定時(shí)字符串的轉(zhuǎn)換成數(shù)字類型的問(wèn)題,需要的朋友可以參考下2017-06-06AngularJS中$watch和$timeout的使用示例
這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過(guò)示例代碼相信更能讓大家理解,有需要的朋友們下面來(lái)一起看看吧。2016-09-09angular4應(yīng)用中輸入的最小值和最大值的方法
這篇文章主要介紹了angular4應(yīng)用中輸入的最小值和最大值的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05AngularJS基礎(chǔ) ng-keypress 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-keypress 指令,這里對(duì)ng-keypress指令的基礎(chǔ)資料整理,并附有實(shí)例代碼,需要的小伙伴參考下2016-08-08