Angular推入数组对象

时间:2016-05-25 08:52:12

标签: javascript angularjs

我正在使用角度日历并举办类似的活动:

$scope.events = [
        //{title: 'test event',start: moment()},
        {title: 'Long Event',start: new Date(y, m, d - 5),end: new Date(y, m, d - 2)},
        {id: 999,title: 'Repeating Event',start: new Date(y, m, d - 3, 16, 0),allDay: false},
        {id: 999,title: 'Repeating Event',start: new Date(y, m, d + 4, 16, 0),allDay: false},
        {title: 'Birthday Party',start: new Date(y, m, d + 1, 19, 0),end: new Date(y, m, d + 1, 22, 30),allDay: false},
        {title: 'Click for Google',start: new Date(y, m, 28),end: new Date(y, m, 29),url: 'http://google.com/'}
    ];

是否有可能动态推送到这一点,例如一个可以继续添加其他事件的按钮?我不能想到将对象推入数组的语法

4 个答案:

答案 0 :(得分:3)

这只是一个javascript数组,因此您使用push

$scope.events.push({
    title:'foo'
});

答案 1 :(得分:0)

这就是你想要的:

$scope.events=[];
var event={title: 'Long Event',start: new Date(y, m, d - 5),end: new Date(y, m, d - 2)};
$scope.events.push(event);

答案 2 :(得分:0)

我们在Array中有.push()方法,你可以将元素推送到数组。

示例:

setInterval(animateManaging,6000);

function animateManaging() {
    // Grabs the canvas element we made above
    var ca1=document.getElementById("cvs3");

    // Defines the 2d thing, standard for making a canvas
    var c1=ca1.getContext("2d");

    // Creates an image variable to hold and preload our image (can't do animations on an image unless its fully loaded)
    var img1 = document.createElement('IMG');

    // Loads image link into the img element we created above
    img1.src = "http://s33.postimg.org/3pk551xcv/managing_values.png";

    // Creates the first save event, this gives us a base to clear our clipping / mask to since you can't just delete elements.
    c1.save();

    // Our function for when the image loads
    img1.onload = function () {
        // clear off the canvas


        // First call to our canvas drawing function, the thing that is going to do all the work for us.
            drawc1r(0);

        // The function that is doing all the heavy lifting. The reason we are doing a function is because
        // to make an animation we have to draw the circle (or element) frame by frame, to do this manually would be to time
        // intensive so we are just going to create a loop to do it. 'i' stands for the radius of our border
        // so over time our radius is going to get bigger and bigger.
        function drawc1r(i) {
            // Creates a save state. Imagine a save state like an array, when you clear one it pops last element in the array off the stack
            // When you save, it creates an element at the top of the stack. So if we cleared without making new ones, we would end up with nothing on our stage.

            c1.save();

            // This clears everything off the stage, I do this because our image has transparency, and restore() (the thing that pops one off the stack)
            // Doesn't clear off images, and so if we stick an image on the stage over and over, the transparency will stack on top of each other and
            // That isn't quite what we want.

            c1.clearRect(0, 0, 386, 380);

            // Adds one to the interval count
            i++;

            // Tells canvas we are going to start creating an item on the stage - it can be a line, a rectangle or any shape you draw, but whatever
            // after this path will be added to the clip when its called. I can have 3 rectangles drawn and that would make a clip.
            c1.beginPath();

            // make the clipping rectangle, using i to make it grow on each interval
            c1.rect(0,0,i*13,380);

            // After everything is defined, we make a clip area out of it.
            c1.clip();

            // Now that we have the clip added to it, we are going to add the image to the clip area.
            c1.drawImage(img1, 0, 0);

            // This pops one off the stack which gets rid of the clip so we can enlarge it and make it again on the next pass
            c1.restore();

            // Here is the final size of the rectangle, I want it to grow until it hits 380 so we set a timeout to run this function again
            // until we get the size we want. The time in milliseconds pretty much defines your framerate


           if (i < 380) {
                setTimeout(function () {
                    drawc1r(i);
                }, 100);
            }
        }
    }
}

与从数组中删除元素类似,我们提供了pop()方法。

答案 3 :(得分:0)

push就像1-2-3一样简单。请参阅下面的代码片段,将新对象推送到现有的AngularJS对象中:

$scope.events.push(newData);

您可以参考demo获取完整代码。