javascript - Push Data Into An Objects Array With Angular JS -


say had following:

.controller("chatctrl", function($scope, $timeout, $rootscope) {   $scope.chats = [{     id: 1,     username: "aname",     avatar: "imgsrc",     messages: [       "hello",       "world"     ]   }]; 

how go pushing data form messages array? have form setup so:

%form{"ng-submit" => "add()"} %input{:type => "text", :placeholder => "enter message", "ng-model" => "text"} %input{:type => "submit", :value => "send"} 

and angular so:

$scope.text = ''; $scope.messages = [];  $scope.add = function() {   if($scope.text) {     $scope.messages.push(this.text);     $scope.text = '';     console.log($scope.messages);   } } 

now of course works because pushing $scope.messages array defined, need submit $scope.chats messages array.

you can use

$scope.text= '';  $scope.add = function() {    $scope.chats[0].messages.push($scope.text); }; 

Comments