angularjs - create a JavaScript object dynamically - Stack Overflow

I want to create a calendar using AngularJS, and my model is an object like this:$scope.model = {weeks:

I want to create a calendar using AngularJS, and my model is an object like this:

$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"}]
                    }
                ]
                }

            ]
           }

But I want to create the object dynamically.

I've tried something like this, but it gives me the following error:

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"}]}]);
            }
        }
    }

Can anyone help me with this? Thanks in advance!

I want to create a calendar using AngularJS, and my model is an object like this:

$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"}]
                    }
                ]
                }

            ]
           }

But I want to create the object dynamically.

I've tried something like this, but it gives me the following error:

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"}]}]);
            }
        }
    }

Can anyone help me with this? Thanks in advance!

Share Improve this question asked Sep 30, 2013 at 15:42 TenziTenzi 1011 gold badge6 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

the problem is that weeks does not exist. You need to create it first like so:

$scope.model = {};//new object
$scope.model.weeks = [];//new array

After that you do not want to push an empty array, instead push a new object that contains a days array:

var weeksObj = {};//new object
weeksObj.days = [];//new array
$scope.model.weeks.push(weeksObj);

You can then push your days values like so, for null:

weeksObj.days.push(null);

and for your days object, again create a new object:

var dayObj = {};//new object
dayObj.name = "name";//set name property
dayObj.toDoItems = [];//new array
weeksObj.days.push(dayObj);

Hopefully that will help you see where your problems are. It seems you are getting mixed up a bit between an object ({}) and an array ([]). Only arrays have the push method

In your loop do not push an empty array but set weeks to an empty array. and do not forget to create first object with day

    for (var i = 0; i < (nrOfDays + gap) % 7; i++) {
        for (var j = 0; j < 7; j++) {
            if (j === 0)
                $scope.model.weeks = [];

            if (i === 0 && j < gap)
                $scope.model.weeks[i] = {day: [null]};

            else
                $scope.model.weeks[i].days.push([{ name: i + ' ' + j, toDoItems: [{ name: "Task 1" }, { name: "Task 2"}]}]);
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744758638a4592033.html

相关推荐

  • angularjs - create a JavaScript object dynamically - Stack Overflow

    I want to create a calendar using AngularJS, and my model is an object like this:$scope.model = {weeks:

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信