![](/img/trans.png)
[英]On click, push one item from an an array to another, and change text dependent on items in array
[英]Push Item From One Array Into another Array in Angular
我正在使用AngularJS编写应用程序,并想知道如何将项目从一个数组推送到另一个数组。
这是示例代码:
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.addToTasksDone = function() {// IM FAILNG HERE};
$scope.tasksDone = [];
如何将值为“Do the dishes”的项目推送到tasksDone数组?
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
$scope.addToTasksDone = function(index) {// IM FAILNG HERE};
$scope.tasksDone.push($scope.tasks[index]);
}
$scope.tasks.push(yourObject)
这个问题在此之前已经过了
$scope.tasks = [
{ title: "Do the dishes" },
{ title: "Walk the dog" }
];
$scope.tasksDone = [];
for(var i in $scope.tasks){
$scope.tasksDone.push($scope.tasks[i]);
}
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
angular.forEach(tasks , function(value, key) {
this.push(key + ': ' + value);
},tasksDone);
};
您可以使用以下代码将某些特定值推送到新数组
$scope.tasks = [
{title: "Do the dishes"},
{title: "Walk the dog"},
];
$scope.tasksDone = [];
$scope.addToTasksDone = function(specificValue) {
// IM FAILNG HERE};
tasks.forEach( (object) => {
if ( object[title] === specificValue ) {
$scope.tasksDone.push(object);
}
});
}
上面的代码将每个包含值的对象作为特定值推送...您可以通过传递“Do the dishes”值作为参数来调用addToTasksDone。
示例调用
$scope.addToTasksDone("Do the dishes");
问候
阿贾伊
这是正确的代码。
我有一个ng-repeat内的列表所以我必须将$scope.addToTasksDone(index)
更改$scope.addToTasksDone(index)
$scope.addToTasksDone($index)
。
Ofcourse也在HTML
中例如 : ng-click="addToTasksDone($index)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.