您好:在AngularJS中,我的脚湿了。
我正在尝试添加和删除数组中的元素;但是,即时通讯无法达到预期的效果。

foll是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/

10-11 05:34