How to push object into an array using AngularJS

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

    A couple of answers that should work above but this is how i would write it.

    Also, i wouldn't declare controllers inside templates. It's better to declare them on your routes imo.

    add-text.tpl.html

    • {{ text }}

    app.js

    (function() {
    
        function myController($scope) {
            $scope.arrayText = ['hello', 'world'];
            $scope.addText = function(myText) {
                 $scope.arrayText.push(myText);     
            };
        }
    
        angular.module('app', [])
            .controller('myController', myController);
    
    })();
    

提交回复
热议问题