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

fix(ng-bind-html): watch string value instead of wrapper #4087

Merged
merged 1 commit into from
Sep 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/ng/directive/ngBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,15 @@ var ngBindTemplateDirective = ['$interpolate', function($interpolate) {
* @element ANY
* @param {expression} ngBindHtml {@link guide/expression Expression} to evaluate.
*/
var ngBindHtmlDirective = ['$sce', function($sce) {
var ngBindHtmlDirective = ['$sce', '$parse', function($sce, $parse) {
return function(scope, element, attr) {
element.addClass('ng-binding').data('$binding', attr.ngBindHtml);
scope.$watch(attr.ngBindHtml, function ngBindHtmlWatchAction(value) {
element.html($sce.getTrustedHtml(value) || '');

var parsed = $parse(attr.ngBindHtml);
function getStringValue() { return (parsed(scope) || '').toString(); }

scope.$watch(getStringValue, function ngBindHtmlWatchAction(value) {
element.html($sce.getTrustedHtml(parsed(scope)) || '');
});
};
}];
12 changes: 12 additions & 0 deletions test/ng/directive/ngBindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ describe('ngBind*', function() {
expect(angular.lowercase(element.html())).toEqual('<div onclick="">hello</div>');
}));

it('should watch the string value to avoid infinite recursion', inject(function($rootScope, $compile, $sce) {
// Ref: https://github.com/angular/angular.js/issues/3932
// If the binding is a function that creates a new value on every call via trustAs, we'll
// trigger an infinite digest if we don't take care of it.
element = $compile('<div ng-bind-html="getHtml()"></div>')($rootScope);
$rootScope.getHtml = function() {
return $sce.trustAsHtml('<div onclick="">hello</div>');
};
$rootScope.$digest();
expect(angular.lowercase(element.html())).toEqual('<div onclick="">hello</div>');
}));

describe('when $sanitize is available', function() {
beforeEach(function() { module('ngSanitize'); });

Expand Down