如何在AngularJS中将项添加到二维数组中

时间:2023-01-21 21:31:00

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

如何将输入值推送到tasks.name并在$ scope.tasks数组中包含默认状态:false?

HTML

HTML

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

JS (AngularJS)

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()
};

}]);

2 个解决方案

#1


2  

just push it as an object to an tasks array

只需将其作为对象推送到任务数组

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

#2


2  

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

#1


2  

just push it as an object to an tasks array

只需将其作为对象推送到任务数组

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

#2


2  

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