简体   繁体   中英

How to add items to a 2-dimensional array in AngularJS

How do I push the value of input to tasks.name and include a default status: false to the $scope.tasks array?

HTML

<input type="text" ng-model="typeTask">
<button ng-click="updateTasks()">Add task</button>

JS (AngularJS)

var app1 = angular.module('app1', []);
app1.controller('ctrl1', ['$scope', function($scope) {
$scope.typeTask = "test";

$scope.tasks = [
    { 
      name: 'Example task 1',
      status: false
    },
    {
      name: 'Example task 2',
      status: true 
    },
    {
      name: 'Example task 3',
      status: false
    }
];

$scope.updateTasks = function() {
    $scope.tasks.push()
};

}]);

just push it as an object to an tasks array

$scope.updateTasks = function() {
    $scope.tasks.push({  
      name: $scope.typeTask,
      status: false
    })
};
$scope.updateTasks = function() {
    $scope.tasks.push({name:$scope.typeTask, status: false})
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM