How to push object into an array using AngularJS

后端 未结 6 2010
别跟我提以往
别跟我提以往 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条回答
  •  梦毁少年i
    2020-12-29 20:02

    'Push' is for arrays.

    You can do something like this:

    app.js:

    (function() {
    
    var app = angular.module('myApp', []);
    
     app.controller('myController', ['$scope', function($scope) {
    
        $scope.myText = "Let's go";
    
        $scope.arrayText = [
                'Hello',
                'world'
            ];
    
        $scope.addText = function() {
            $scope.arrayText.push(this.myText);
        }
    
     }]);
    
    })();
    

    index.html

    
    
      
        
        
      
      
        
    list={{arrayText}}

提交回复
热议问题