From 8849213ce42fa98169b6fbbb0805feb95a6ba678 Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Wed, 16 Sep 2015 12:26:32 -0700 Subject: [PATCH] fix(autocomplete): re-adds support for custom item names in autocomplete templates Closes #4667 --- .../js/autocompleteParentScopeDirective.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/components/autocomplete/js/autocompleteParentScopeDirective.js b/src/components/autocomplete/js/autocompleteParentScopeDirective.js index 28ede2037f7..a712632b677 100644 --- a/src/components/autocomplete/js/autocompleteParentScopeDirective.js +++ b/src/components/autocomplete/js/autocompleteParentScopeDirective.js @@ -10,17 +10,13 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) { }; function postLink(scope, element, attr) { - var newScope = scope.$mdAutocompleteCtrl.parent.$new(); - var relevantVariables = ['item', '$index']; + var ctrl = scope.$mdAutocompleteCtrl; + var newScope = ctrl.parent.$new(); + var itemName = ctrl.itemName; // Watch for changes to our scope's variables and copy them to the new scope - angular.forEach(relevantVariables, function(variable){ - scope.$watch(variable, function(value) { - $mdUtil.nextTick(function() { - newScope[variable] = value; - }); - }); - }); + watchVariable('$index', '$index'); + watchVariable('item', itemName); // Recompile the contents with the new/modified scope $compile(element.contents())(newScope); @@ -30,5 +26,17 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) { element.after(element.contents()); element.remove(); } + + /** + * Creates a watcher for variables that are copied from the parent scope + * @param variable + * @param alias + */ + function watchVariable (variable, alias) { + $mdUtil.nextTick(function () { + newScope[alias] = scope[variable]; + scope.$watch(variable, function (value) { newScope[alias] = value; }); + }); + } } } \ No newline at end of file