我正在尝试使用AngularJS将两个文本字段的值传递到数组中,但我还想检查传递的第一个值是否已经存在于该数组中。如果它已经存在于数组中,我不想再添加它,如果不存在,则添加这些值。我可以将这些值添加进去,但是在尝试检查姓氏值是否已经存在于该数组中时出现了困难。我找到了一些示例,尝试按照它们进行操作,但似乎无法使其正常工作。感谢您的帮助!
<div class="panel-body">
<div ng-controller="TodoCtrl">
<ul>
<li li ng-repeat="todo in todos">
{{todo.text}}, {{todo.name}}
</li>
</ul>
<form class="form-horizontal">
<input type="text" ng-model="formTodoLast" ng-model-instant>
<input type="text" ng-model="formTodoFirst" ng-model-instant>
<button class="btn" ng-click="addTodo()"><i class="icon-plus"></i>Add</button>
</div>
</div>
todo.js
function TodoCtrl($scope) {
$scope.todos = [];
$scope.addTodo = function () {
if($scope.todos.indexOf(text:$scope.formTodoLast) == -1) {
$scope.todos.push({text:$scope.formTodoLast, name:$scope.formTodoFirst});
$scope.formTodoText = ' ';
$scope.formTodoName = ' ';
}
}
}