Skip to content

Commit 8849213

Browse files
author
Robert Messerle
committed
fix(autocomplete): re-adds support for custom item names in autocomplete
templates Closes angular#4667
1 parent 77a34bd commit 8849213

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/components/autocomplete/js/autocompleteParentScopeDirective.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) {
1010
};
1111

1212
function postLink(scope, element, attr) {
13-
var newScope = scope.$mdAutocompleteCtrl.parent.$new();
14-
var relevantVariables = ['item', '$index'];
13+
var ctrl = scope.$mdAutocompleteCtrl;
14+
var newScope = ctrl.parent.$new();
15+
var itemName = ctrl.itemName;
1516

1617
// Watch for changes to our scope's variables and copy them to the new scope
17-
angular.forEach(relevantVariables, function(variable){
18-
scope.$watch(variable, function(value) {
19-
$mdUtil.nextTick(function() {
20-
newScope[variable] = value;
21-
});
22-
});
23-
});
18+
watchVariable('$index', '$index');
19+
watchVariable('item', itemName);
2420

2521
// Recompile the contents with the new/modified scope
2622
$compile(element.contents())(newScope);
@@ -30,5 +26,17 @@ function MdAutocompleteItemScopeDirective($compile, $mdUtil) {
3026
element.after(element.contents());
3127
element.remove();
3228
}
29+
30+
/**
31+
* Creates a watcher for variables that are copied from the parent scope
32+
* @param variable
33+
* @param alias
34+
*/
35+
function watchVariable (variable, alias) {
36+
$mdUtil.nextTick(function () {
37+
newScope[alias] = scope[variable];
38+
scope.$watch(variable, function (value) { newScope[alias] = value; });
39+
});
40+
}
3341
}
3442
}

0 commit comments

Comments
 (0)