Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit eda4782

Browse files
author
Robert Messerle
committed
fix(highlightText): changes to the template data will update the generated HTML
Closes #3550
1 parent 2901bd1 commit eda4782

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('<md-autocomplete>', function () {
1111
return container;
1212
}
1313

14-
function createScope (items) {
14+
function createScope (items, obj) {
1515
var scope;
1616
items = items || [ 'foo', 'bar', 'baz' ].map(function (item) { return { display: item }; });
1717
inject(function ($rootScope) {
@@ -23,6 +23,7 @@ describe('<md-autocomplete>', function () {
2323
};
2424
scope.searchText = '';
2525
scope.selectedItem = null;
26+
for (var key in obj) scope[key] = obj[key];
2627
});
2728
return scope;
2829
}
@@ -302,4 +303,25 @@ describe('<md-autocomplete>', function () {
302303
expect(scope.selectedItem).toBe(null);
303304
}));
304305
});
306+
307+
describe('md-highlight-text', function () {
308+
it('should update when content is modified', inject(function () {
309+
var template = '<div md-highlight-text="query">{{message}}</div>';
310+
var scope = createScope(null, { message: 'some text', query: 'some' });
311+
var element = compile(template, scope);
312+
313+
expect(element.html()).toBe('<span class="highlight">some</span> text');
314+
315+
scope.query = 'so';
316+
scope.$apply();
317+
318+
expect(element.html()).toBe('<span class="highlight">so</span>me text');
319+
320+
scope.message = 'some more text';
321+
scope.$apply();
322+
323+
expect(element.html()).toBe('<span class="highlight">so</span>me more text');
324+
}));
325+
});
326+
305327
});

src/components/autocomplete/js/highlightController.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@ angular
33
.controller('MdHighlightCtrl', MdHighlightCtrl);
44

55
function MdHighlightCtrl ($scope, $element, $interpolate) {
6-
this.init = init;
6+
var ctrl = this;
77

8-
return init();
8+
ctrl.term = null;
9+
ctrl.template = null;
10+
ctrl.watchers = [];
11+
ctrl.init = init;
912

10-
function init (term) {
11-
var unsafeText = $interpolate($element.html())($scope),
13+
function init (term, template) {
14+
createWatchers(term, template);
15+
$element.on('$destroy', cleanup);
16+
}
17+
18+
function createWatchers (term, template) {
19+
ctrl.watchers.push($scope.$watch(term, function (term) {
20+
ctrl.term = term;
21+
updateHTML(term, ctrl.template);
22+
}));
23+
ctrl.watchers.push($scope.$watch(compileTemplate, function (template) {
24+
ctrl.template = template;
25+
updateHTML(ctrl.term, template);
26+
}));
27+
28+
function compileTemplate () { return $interpolate(template)($scope); }
29+
}
30+
31+
function cleanup () {
32+
ctrl.watchers.forEach(function (watcher) { watcher(); });
33+
}
34+
35+
function updateHTML () {
36+
if (ctrl.term === null || ctrl.template === null) return;
37+
var unsafeText = $interpolate(ctrl.template)($scope),
1238
text = angular.element('<div>').text(unsafeText).html(),
1339
flags = $element.attr('md-highlight-flags') || '',
14-
watcher = $scope.$watch(term, function (term) {
15-
var regex = getRegExp(term, flags),
16-
html = text.replace(regex, '<span class="highlight">$&</span>');
17-
$element.html(html);
18-
});
19-
$element.on('$destroy', function () { watcher(); });
40+
regex = getRegExp(ctrl.term, flags),
41+
html = text.replace(regex, '<span class="highlight">$&</span>');
42+
$element.html(html);
2043
}
2144

2245
function sanitize (term) {

src/components/autocomplete/js/highlightDirective.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ function MdHighlight () {
3636
terminal: true,
3737
scope: false,
3838
controller: 'MdHighlightCtrl',
39-
link: function (scope, element, attr, ctrl) {
40-
ctrl.init(attr.mdHighlightText);
39+
compile: function (element, attr) {
40+
var template = element.html();
41+
return function (scope, element, attr, ctrl) {
42+
ctrl.init(attr.mdHighlightText, template);
43+
};
4144
}
4245
};
4346
}

0 commit comments

Comments
 (0)