Angularjs 手寫(xiě)日歷的實(shí)現(xiàn)代碼(不用插件)
本文介紹了Angularjs 手寫(xiě)日歷的實(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)開(kāi)始添加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在控制器(controller.js)中使用過(guò)濾器($filter)格式化日期/時(shí)間實(shí)例
本篇文章主要介紹了詳解Angularjs在控制器(controller.js)中使用過(guò)濾器($filter)格式化日期/時(shí)間實(shí)例 ,有需要的小伙伴可以參考下。2017-02-02
掌握Queries設(shè)計(jì)模式優(yōu)化Angular應(yīng)用開(kāi)發(fā)技巧
這篇文章主要介紹了掌握Queries設(shè)計(jì)模式優(yōu)化Angular應(yīng)用開(kāi)發(fā)的技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼
本篇文章主要介紹了Angular使用 ng-img-max 調(diào)整瀏覽器中的圖片的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
AngularJS路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)實(shí)例
這篇文章主要為大家詳細(xì)介紹了AngularJS路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
AngularJS 限定$scope的范圍實(shí)例詳解
這篇文章主要介紹了AngularJS 限定$scope的范圍實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
詳解基于angular-cli配置代理解決跨域請(qǐng)求問(wèn)題
本篇文章主要介紹了詳解基于angular-cli配置代理解決跨域請(qǐng)求問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

