gpt4 book ai didi

javascript - 无法在 AngularJS 的数组中添加或删除元素

转载 作者:行者123 更新时间:2023-11-30 16:10:43 25 4
gpt4 key购买 nike

你好:我正在接触 AngularJS。我正在尝试添加和删除数组中的元素;但是,我无法达到预期的效果。

下面是JSON结构:

    $scope.masters = [
{
"name": "Wittgenstein",
"thoughts": ["thought 1", "thought 2", "thought 3"]

},

{
"name": "King",
"thoughts": ["thought 1", "thought 2", "thought 3"]

}
];

傻瓜就是骗子。

Plunker

非常感谢任何输入。谢谢。

最佳答案

按照@Mathew的建议,你应该介绍一下$index的用法,如下所示:

JS代码:

$scope.input = [];

$scope.add = function(i) { // receiving index as param
$scope.masters[i].thoughts.push($scope.input[i]);
$scope.input[i] = '';
};

HTML代码:

<div ng-repeat="master in masters">
<h1>{{ master.name }}</h1>
<ul>
<li ng-repeat="thought in master.thoughts">
{{ thought }} <button ng-click="remove($index)">Remove</button>
</li>
</ul>
<input type="text" ng-model="input[$index]">
<button type="submit" ng-click="add($index)">Add</button>
</div>

看到这个 Plunker working example

关于javascript - 无法在 AngularJS 的数组中添加或删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com