in my todo list i dont want user to input same todos again again... but my problem is, when i enter something for example (test) first time and than i enter (test2) and than i enter (test) again, so its taking a value.... how to validate properly....
fiddle https://jsfiddle.net/LaL7h6Lv/1/
html
<div ng-app="todoApp" ng-controller="mainCtrl"> <ul> <li ng-repeat="todoItem in todoItems">{{todoItem.name}}</li> </ul> <form ng-submit="addItem()"> <input type="text" ng-model="newItem"> <input type="submit" name="go"> </form> </div>
angularjs
angular.module("todoApp", []) .controller('mainCtrl', ['$scope', function($scope){ $scope.todoItems = [{'name' : 'akshay'}]; $scope.test = false; $scope.addItem = function(){ if($scope.newItem){ $scope.checkRepeatTodo(); if($scope.test == true){ $scope.todoItems.push({'name':$scope.newItem}); $scope.newItem = ''; }else{ alert('same todo'); $scope.test = false; } }else{ alert('fill the form'); } }; $scope.checkRepeatTodo = function(){ $scope.todoItems.filter(function(item){ if($scope.newItem === item.name){ $scope.test = false; }else{ $scope.test = true; } }); }; }]);