0

I am using ng-repeat for printing the HTML input element. I want to check that when I add new HTML input element it should not contain the same value as compared with the previous one.

This is my code.

<div class="row" ng-repeat="m in machines">
    <div id="machinename_{{$index}}">
        <input id="machinenameInput_{{$index}}" type="text" class="form-control" maxlength="20" ng-model="m.alias" required>
    </div>
</div>

In the above code when i add new machine name it should not contain the same name.Thank you in advance

George
  • 6,630
  • 2
  • 29
  • 36
  • Do you just care about the previous record or would you like to compare against the entire collection? – George Nov 02 '17 at 09:57
  • Probably you need something like `unique` filter for you machines ? Check this question https://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results – Telman Nov 02 '17 at 10:01

1 Answers1

0

use ng-if to display the machine only if the previous machine has a different value

<div class="row" ng-repeat="m in machines">
    <div ng-if="machines[$index-1] != machines[$index]" id="machinename_{{$index}}">
         <input id="machinenameInput_{{$index}}" type="text" class="form-control" maxlength="20" ng-model="m.alias" required>
     </div>
</div>
AnatPort
  • 748
  • 8
  • 20
  • It is recommended to use `ng-if` over `ng-show` / `ng-hide` because `ng-show` basically hides the element from the DOM (same as using `display: none;`). `ng-if` adds the element to the DOM when the condition is true otherwise it removes it – Georgian-Sorin Maxim Nov 02 '17 at 09:59
  • I'm assuming this is in a form where you enter data, so neither `ng-if` or `ng-show` would be a good idea as it would just hide the elements but not actually remove them from the collection. I _think_ what the OP wants is validation on the form, it's a bit hard to tell from the context though. – George Nov 02 '17 at 10:05
  • @AnatPort Thank you..:). – Avadhut Lakule Nov 06 '17 at 04:58