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

时间:2017-05-16 18:11:14

标签: javascript angularjs arrays

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

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

}]);

2 个答案:

答案 0 :(得分:2)

将它作为对象推送到tasks数组

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

答案 1 :(得分:2)

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