我想創建使用AngularJS
日曆,我的模型是這樣的對象:創建JavaScript對象動態
$scope.model = {
weeks: [
{
days: [
null,
{
name: "3 dec",
toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]
},
{
name: "4 dec",
toDoItems: [{name: "Task 1"}, {name: "Task 2"}]
}
]
},
{
days: [
null,
{
name: "5 dec",
toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]
},
{
name: "6 dec",
toDoItems: [{name: "Task 1"}, {name: "Task 2"}]
}
]
}
]
}
但我要動態地創建對象。
我已經試過這樣的事情,但它給了我下面的錯誤:
TypeError: Cannot call method 'push' of undefined
$scope.fillMonth = function() {
var gap = dayInWeek($scope.year.value, $scope.month, 1),
nrOfDays = daysInMonth($scope.year.value, $scope.month);
$scope.model = {};
for (var i = 0; i < (nrOfDays + gap) % 7; i++) {
for (var j = 0; j < 7; j++) {
if (j === 0)
$scope.model.weeks.push([]);
if (i === 0 && j < gap)
$scope.model.weeks[i].days.push(null);
else
$scope.model.weeks[i].days.push([{ name: i + ' ' + j, toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]}]);
}
}
}
誰能幫助我?提前致謝!
周必須設置。 (我只是編輯我的答案) – peernohell