How to push object into an array using AngularJS

后端 未结 6 2012
别跟我提以往
别跟我提以往 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 19:53

    Please check this - http://plnkr.co/edit/5Sx4k8tbWaO1qsdMEWYI?p=preview

    Controller-

    var app= angular.module('app', []);
    
    app.controller('TestController', function($scope) {
        this.arrayText = [{text:'Hello',},{text: 'world'}];
    
        this.addText = function(text) {
    
          if(text) {
            var obj = {
              text: text
            };
              this.arrayText.push(obj);
              this.myText = '';
              console.log(this.arrayText);
            }
          } 
     });
    

    HTML

    {{item}}

提交回复
热议问题