Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depends on what your goal is. </p> <p>If you want to allow for duplicate values you need to change the code a bit as each item in the ngRepeat has to have a unique id. See the <code>track by</code> section <a href="http://docs.angularjs.org/api/ng.directive%3angRepeat" rel="nofollow">here</a>.</p> <p>That would work like this:</p> <pre><code>&lt;div ng-app="myApp"&gt; &lt;div ng-controller="myCtrl"&gt; &lt;input type="text" ng-model="item" /&gt; &lt;div ng-repeat="item in items"&gt; {{ item.value }} &lt;/div&gt; &lt;button ng-click="save()"&gt;Save&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;script&gt; angular.module('myApp', []). controller('myCtrl', function ($scope) { $scope.items = []; $scope.save = function() { $scope.items.push({value:$scope.item}); } }); &lt;/script&gt; </code></pre> <p>See the updated fiddle <a href="http://jsfiddle.net/u8Fuk/9/" rel="nofollow">here</a>.</p> <p>If you don't want to allow for the same values you need to search for it. </p> <pre><code>&lt;div ng-app="myApp"&gt; &lt;div ng-controller="myCtrl"&gt; &lt;input type="text" ng-model="item" /&gt; &lt;div ng-repeat="item in items"&gt; {{ item }} &lt;/div&gt; &lt;button ng-click="save()"&gt;Save&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;script&gt; angular.module('myApp', []). controller('myCtrl', function ($scope) { $scope.items = []; $scope.save = function() { var found = $scope.items.reduce(function(previous, i){ if ($scope.item === i) return true; return previous; }, false); if (found){ alert('duplicate value'); } else{ $scope.items.push($scope.item); } } }); &lt;/script&gt; </code></pre> <p>See the updated fiddle <a href="http://jsfiddle.net/u8Fuk/10/" rel="nofollow">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload