Skip to content

Commit

Permalink
fix(autocomplete): re-adds support for custom item names in autocomplete
Browse files Browse the repository at this point in the history
templates

Closes angular#4667
  • Loading branch information
Robert Messerle committed Sep 16, 2015
1 parent 77a34bd commit 8849213
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/components/autocomplete/js/autocompleteParentScopeDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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; });
});
}
}
}

0 comments on commit 8849213

Please sign in to comment.