Angularjs 手寫日歷的實現(xiàn)代碼(不用插件)
更新時間:2017年10月18日 08:22:00 作者:壬萬er
本篇文章介紹了Angularjs 手寫日歷的實現(xiàn)代碼(不用插件),整理了詳細的代碼,非常具有實用價值,需要的朋友可以參考下
本文介紹了Angularjs 手寫日歷的實現(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)
//初始化顯示 當前年和月
$scope.show_now()
}
//當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日的那個時間對象
var date = new Date(year, month - 1, 1);
//1.先添加響應的空白的li:這個月1號是星期幾,就添加幾個空白的li
var dayOfWeek = date.getDay(); //得到1日是星期幾
for(var i = 0; i < dayOfWeek; i++) {
$scope.days.push("");
}
//計算一個月有多少天
var daysOfMonth = $scope.calDays(year, month);
//2. 從1號開始添加li
for(var i = 1; i <= daysOfMonth; i++) {
$scope.days.push(i)
}
}
$scope.active_day = ''
$scope.select_year = ''
$scope.select_month = ''
//初始化顯示 當前年和月
$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)建日歷
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
angularjs中使用ng-bind-html和ng-include的實例
下面小編就為大家?guī)硪黄猘ngularjs中使用ng-bind-html和ng-include的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
自定義Angular指令與jQuery實現(xiàn)的Bootstrap風格數(shù)據(jù)雙向綁定的單選與多選下拉框
這篇文章主要介紹了自定義Angular指令與jQuery實現(xiàn)的Bootstrap風格數(shù)據(jù)雙向綁定的單選與多選下拉框 的相關資料,需要的朋友可以參考下2015-12-12
angularJs使用ng-repeat遍歷后選中某一個的方法
今天小編就為大家分享一篇angularJs使用ng-repeat遍歷后選中某一個的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
如何在Angular8.0下使用ngx-translate進行國際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進行國際化配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
Angularjs 雙向綁定時字符串的轉換成數(shù)字類型的問題
這篇文章主要介紹了Angular js雙向綁定時字符串的轉換成數(shù)字類型的問題,需要的朋友可以參考下2017-06-06
AngularJS中$watch和$timeout的使用示例
這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過示例代碼相信更能讓大家理解,有需要的朋友們下面來一起看看吧。2016-09-09
AngularJS基礎 ng-keypress 指令簡單示例
本文主要介紹AngularJS ng-keypress 指令,這里對ng-keypress指令的基礎資料整理,并附有實例代碼,需要的小伙伴參考下2016-08-08

