Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(directive): trim off white space around element.text()
Browse files Browse the repository at this point in the history
Using the directive on an element containing white space results in 'Translation for ... doesn't exist'.
Changing the behaviour of the directive to trim off white space fixes this issue.
As trim() is unsupported on IE 8, replace() is chosen.
  • Loading branch information
gjungb committed May 12, 2013
1 parent 38919e9 commit e10173a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ngTranslate/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ angular.module('ngTranslate')

// Ensures any change of the attribute "translate" containing the id will
// be re-stored to the scope's "translationId".
// If the attribute has no content, the element's text value will be used.
// If the attribute has no content, the element's text value (white spaces trimmed off) will be used.
attr.$observe('translate', function (translationId) {
if (angular.equals(translationId , '')) {
scope.translationId = $interpolate(element.text())(scope.$parent);
scope.translationId = $interpolate(element.text().replace(/^\s+|\s+$/g,''))(scope.$parent);
} else {
scope.translationId = translationId;
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/translateDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ describe('ngTranslate', function () {
expect(element.text()).toBe('foo');
});
});

it('should return translation if translation id exists and if its passed surrounded by white space', function () {
inject(function ($rootScope, $compile) {
element = $compile('<div translate> TRANSLATION_ID </div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');
});
});
});

describe('Passing values', function () {
Expand Down

0 comments on commit e10173a

Please sign in to comment.