我想在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;
}
});
我不确定如何链接它们,因此任何帮助都会很大,因为我仍在学习并且找不到类似的问题或示例。
最佳答案
这个问题缺少一些细节,但这就是我要怎么做。
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以匹配日期,并且可以遍历所有日期并提取所需的日期。
关于javascript - 使用带有getDay()方法的switch语句?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45803892/