当星期天是星期天时,我会收到错误。星期一到星期六,当星期天时,它会正常工作
错误:插值时出错:{{isOpen(dealer.Day)}}
TypeError:无法读取未定义的属性“替换”
var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
var now = new Date(ds);
如果您更改日期和日期可以正常工作
var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
var now = new Date(ds);
我在下面添加了我的代码,请帮帮我。
angular.module('myApp', [])
.controller("myCntrl", function($scope, $filter) {
$scope.isOpen = function(dealerSchedule) {
var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
var now = new Date(ds);
//var now = new Date();
//---if you change day and date you will not get error
//var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
//var now = new Date(ds);
var times = dealerSchedule[Object.keys(dealerSchedule)[now.getDay() - 1]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");
var nowTime = new Date('1/1/1900 ' + now.toLocaleTimeString(navigator.language, {
hour: '2-digit',
minute: '2-digit',
hour12: true
}));
console.log(nowTime);
var response = (times == "Leave" ? "Leave" : (nowTime >= new Date(times[0]) && nowTime <= new Date(times[1]) ? "Open Now" : "Closed Now"));
return response;
};
$scope.dealers = [{
S_Email_id: "[email protected] ",
status: "",
Store_Name: "Adtiya Samsung Store",
Day: {
"monday": "09:10 AM - 06:30 PM",
"tuesday": "09:10 AM - 12:00 PM",
"wednesday": "09:10 AM - 06:30 PM",
"thursday": "09:10 AM - 06:30 PM",
"friday": "09:10 AM - 06:30 PM",
"saturday": "10:15 AM - 04:15 PM",
"sunday": "10:15AM - 04:15PM"
},
}, {
S_Email_id: "[email protected]",
status: "",
Store_Name: "sri shakthi mobile service",
Day: {
"monday": "09:00 AM - 06:00 PM",
"tuesday": "09:00 AM - 06:00 PM",
"wednesday": "09:00 AM - 06:00 PM",
"thursday": "09:00 AM - 06:00 PM",
"friday": "09:00 AM - 06:00 PM",
"saturday": "09:00AM - 06:00PM",
"sunday": "Leave"
},
}, {
S_Email_id: "[email protected]",
status: "",
Store_Name: "sun mobile service center ",
Day: {
"monday": "08:30 AM - 07:30 PM",
"tuesday": "02:30 PM - 07:30 PM",
"wednesday": "08:30 AM - 07:30 PM",
"thursday": "08:30 AM - 07:30 PM",
"friday": "08:30 AM - 07:30 PM",
"saturday": "08:15 AM - 02:15 PM",
"sunday": "8:15 AM - 12:15AM"
},
}, {
S_Email_id: "[email protected] ",
status: "",
Store_Name: "ragu mobile service center ",
Day: {
"monday": "10:00 AM - 10:00 PM",
"tuesday": "10:00 AM - 10:00 PM",
"wednesday": "10:00 AM - 04:00 PM",
"thursday": "10:00 AM - 10:00 PM",
"friday": "10:00 AM - 10:00 PM",
"saturday": "leave",
"sunday": "leave"
},
}]
//var date = new Date();
//$scope.hhmmsstt = $filter('date')(new Date(), 'hh:mm:ss a');
//console.log($scope.hhmmsstt);
})
//]]>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js'></script>
<div ng-app="myApp">
<div ng-controller="myCntrl">
<label>Search on Label</label>
<br>
<input ng-model="query" type="text" placeholder="Search for name" />
<br>
<br>
<div ng-repeat="dealer in dealers">
{{dealer.Store_Name}}
<br>{{dealer.S_Email_id}}
<br>{{dealer.Day}}
<br>
<input type="button" value="order" />
<span>{{isOpen(dealer.Day)}}</span>
<br>
<br>
<br>
</div>
</div>
</div>
检查我的代码我遇到错误
请不要拒绝投票,我不知道为什么它不起作用我是技术新手。
最佳答案
问题出在now.getDay() - 1
将在星期日失败(getday()将返回0)
您应该检查类似的东西,
var dayCheck = now.getDay() == 0 ? 6 : now.getDay() - 1;
var times = dealerSchedule[Object.keys(dealerSchedule)[dayCheck]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");
关于javascript - “在我的代码中使用angularjs插值{{”时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34248562/