Determines the index of a value
in an array
.
Note: This function is used to augment the Array
type in Angular expressions. See
angular.Array
for more information about Angular arrays.
angular.Array.indexOf(array, value);
array – {Array} –
Array to search.
value – {*} –
Value to search for.
{number}
– The position of the element in array
. The position is 0-based.
If the value cannot be found, -1
is returned.
<script> function Ctrl() { this.books = ['Moby Dick', 'Great Gatsby', 'Romeo and Juliet']; this.bookName = 'Romeo and Juliet'; } </script> <div ng:controller="Ctrl"> <input ng:model='bookName'> <br> Index of '{{bookName}}' in the list {{books}} is <em>{{books.$indexOf(bookName)}}</em>. </div>
it('should correctly calculate the initial index', function() { expect(binding('books.$indexOf(bookName)')).toBe('2'); }); it('should recalculate', function() { input('bookName').enter('foo'); expect(binding('books.$indexOf(bookName)')).toBe('-1'); input('bookName').enter('Moby Dick'); expect(binding('books.$indexOf(bookName)')).toBe('0'); });