我使用角度js来生成添加数组并在我们继续时显示它。到目前为止,给定的代码在列表中添加了一个数组,但它不会在按下函数的情况下继续添加数组。
我对角度很陌生,我认为有一些我想念的东西,但现在我被卡住了。
html代码有一个链接的按钮,用于执行该功能。
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = ["Testing Task 5.1", "Downloaded Task 5.1"];
$scope.addArray = function() {
$scope.records.unshift("saddfadffas")
}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myCtrl" class="container">
<p>
<h3>Status: </h3>
<input type="text" ng-model="additem">
<button ng-click="addArray()">function</button>
</p>
<ul ng-repeat="x in records">
<li>{{x}}</li>
</ul>
</div>
&#13;
答案 0 :(得分:3)
Try push
rather thanunshift
? Sorry I am not at my computer right now so can't verify this, but I'm pretty sure this will solve the problem
答案 1 :(得分:3)
您可以通过让数组中的项按其位置而不是其值来键入来解决此问题。为此,您可以使用track by $index
。希望这有帮助!
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = ["Testing Task 5.1", "Downloaded Task 5.1"];
$scope.addArray = function() {
$scope.records.unshift("saddfadffas")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl" class="container">
<p>
<h3>Status: </h3>
<input type="text" ng-model="additem">
<button ng-click="addArray()">function</button>
</p>
<ul ng-repeat="x in records track by $index">
<li>{{x}}</li>
</ul>
</div>
答案 2 :(得分:1)
$scope.records.unshift($scope.additem)
而不是
$scope.records.unshift("saddfadffas")
但我假设您可能遇到了一个我们在您发布的摘录中看不到的错误。