我想使用带有getDay()
方法的switch语句。
这是我正在使用的switch语句。我希望根据当天返回不同的数据。
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
这是我想要在星期一返回的数据示例。
$scope.suggest1("monday-id", function(sItem) {
if (sItem !== null) {
$scope.suggestion = sItem.id;
$scope.name = sItem.name;
$scope.price = sItem.price;
}
});
我不确定如何将它们联系起来,所以任何帮助都会很棒,因为我还在学习,找不到类似的问题或例子。
答案 0 :(得分:0)
这个问题缺少一些细节,但这就是我要做的。
angular.module('myapp', []).controller('myctrl', [
'$scope', function($scope) {
var getDay, getDayDetails;
getDayDetails = function(day) {
var details;
details = [
{
0: {name: 'sun',price: '0'},
1: {name: 'mon',price: '1'},
2: {name: 'tue',price: '2'},
3: {name: 'wed',price: '3'},
4: {name: 'thu',price: '4'},
5: {name: 'fri',price: '5'},
6: {name: 'sat',price: '6'}
}
];
return $scope.todayDetails = details[0][day];
};
getDay = function(date) {
console.log("called");
switch (date) {
case 0:
getDayDetails(0);
break;
case 1:
getDayDetails(1);
break;
case 2:
getDayDetails(2);
break;
case 3:
getDayDetails(3);
break;
case 4:
getDayDetails(4);
break;
case 5:
getDayDetails(5);
break;
case 6:
getDayDetails(6);
}
};
$scope.getDay = function(day) {
$scope.day = getDay(day);
return console.log("scope.day is ", $scope.day);
};
getDay(new Date().getDay());
}
]);
<div ng-app="myapp" ng-controller="myctrl">
<pre>{{todayDetails}}</pre>
</div>
(html只是一个例子,只是为了给你一个想法) 这是笔: https://codepen.io/nickimola/pen/zdjGGr?editors=0010
或详细信息也可以不是数组,您在对象中拥有与当天匹配的ID,您可以遍历所有日期并提取所需的日期。