這是您的plnkr的更新版本。
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="[email protected]" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="demoController">
<div ng-repeat="master in masters track by $index">
<h1>{{ master.name }}</h1>
<ul>
<li ng-repeat="thought in master.thoughts track by $index">
{{ thought }} <button ng-click="remove(master, $index)">Remove</button>
</li>
</ul>
<input type="text" ng-model="input[$index]">
<button type="submit" ng-click="add($index)">Add</button>
</div>
</body>
</html>
請注意,我是如何將當前主作爲參數包含在remove函數中的。這確保了由於JavaScript數組通過引用傳遞而正確的主拼接。以下是更新後的角度控制器。
var app = angular.module('app', []);
app.controller('demoController', ['$scope', function ($scope) {
$scope.input = [];
$scope.masters = [ {
"name": "Wittgenstein",
"thoughts": ["thought 1", "thought 2", "thought 3"]
}, {
"name": "King",
"thoughts": ["thought 1", "thought 2", "thought 3"]
}];
$scope.add = function(index) {
$scope.masters[index].thoughts.push($scope.input[index]);
$scope.input[index] = '';
};
$scope.remove = function(master, index) {
master.thoughts.splice(index, 1);
};
}]);
的附加功能似乎並沒有被採摘從輸入模型中的價值,但後來應該是具有約束力的問題,而不是不工作,因爲它應該的功能。
Working Plunkr
我希望這有助於。
@Matthew你是指這樣的sthg;雖然它不工作:'$ scope.add = function(){ $ scope.masters.thoughts [$ index] .push($ scope.input); $ scope.input =''; };' – Nosail