How to push object into an array using AngularJS

后端 未结 6 2036
别跟我提以往
别跟我提以往 2020-12-29 19:18

I am trying to do use the angular push function but it is not working.

I want to add strings (or objects) into an array.

I searched for basic examples here

6条回答
  •  不知归路
    2020-12-29 20:05

      var app = angular.module('myApp', []);
            app.controller('myCtrl', function ($scope) {
    
                //Comments object having reply oject
                $scope.comments = [{ comment: 'hi', reply: [{ comment: 'hi inside commnet' }, { comment: 'hi inside commnet' }] }];
    
                //push reply
                $scope.insertReply = function (index, reply) {
                    $scope.comments[index].reply.push({ comment: reply });
                }
    
                //push commnet
                $scope.newComment = function (comment) {
                    $scope.comments.push({ comment:comment, reply: [] });
                }
            });
    
    
    • Comment {{$index}} :
      {{comment.comment}}
      • Reply {{$index}} :
        {{reply.comment}}
      Reply
    New comment Post

提交回复
热议问题