From 8611877ef43581fe493fd3195726f027af2ae3cc Mon Sep 17 00:00:00 2001 From: Michael Benford Date: Fri, 20 Mar 2015 02:17:54 -0300 Subject: [PATCH] feat(autocomplete): Make $index available to custom templates Make the suggestion index available to custom templates via the local variable $index. --- src/auto-complete-match.js | 5 +++-- test/auto-complete.spec.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/auto-complete-match.js b/src/auto-complete-match.js index 4655e276..0ea3908a 100644 --- a/src/auto-complete-match.js +++ b/src/auto-complete-match.js @@ -12,13 +12,14 @@ tagsInput.directive('tiAutocompleteMatch', function($sce, tiUtil) { return { restrict: 'E', require: '^autoComplete', - template: '', + template: '', scope: { data: '=' }, link: function(scope, element, attrs, autoCompleteCtrl) { var autoComplete = autoCompleteCtrl.registerAutocompleteMatch(), options = autoComplete.getOptions(); - scope.template = options.template; + scope.$$template = options.template; + scope.$index = scope.$parent.$index; scope.$highlight = function(text) { if (options.highlightMatchedText) { diff --git a/test/auto-complete.spec.js b/test/auto-complete.spec.js index 95c2f1d1..e8653257 100644 --- a/test/auto-complete.spec.js +++ b/test/auto-complete.spec.js @@ -1282,6 +1282,19 @@ describe('autoComplete directive', function() { expect(getSuggestionScope(2).data).toEqual({ id: 3, text: 'Item3', image: 'item3.jpg' }); }); + it('makes suggestions\' indexes available to the template', function() { + // Arrange + compile(); + + // Act + loadSuggestions(3); + + // Assert + expect(getSuggestionScope(0).$index).toBe(0); + expect(getSuggestionScope(1).$index).toBe(1); + expect(getSuggestionScope(2).$index).toBe(2); + }); + it('makes helper functions available to the template', function() { // Arrange compile();