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

Commit

Permalink
fix(directive): missing watch for expression within elements text nodes
Browse files Browse the repository at this point in the history
Fixes #701
  • Loading branch information
knalli committed Dec 7, 2014
1 parent 4f6ee9a commit 31c0356
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ angular.module('pascalprecht.translate')

var translateValueExist = tElement[0].outerHTML.match(/translate-value-+/i);

var interpolateRegExp = "^(.*)(" + $interpolate.startSymbol() + ".*" + $interpolate.endSymbol() + ")(.*)";
var interpolateRegExp = "^(.*)(" + $interpolate.startSymbol() + ".*" + $interpolate.endSymbol() + ")(.*)",
watcherRegExp = "^(.*)" + $interpolate.startSymbol() + "(.*)" + $interpolate.endSymbol() + "(.*)";

return function linkFn(scope, iElement, iAttr) {

Expand All @@ -121,6 +122,10 @@ angular.module('pascalprecht.translate')
scope.preText = interpolateMatches[1];
scope.postText = interpolateMatches[3];
translationIds.translate = $interpolate(interpolateMatches[2])(scope.$parent);
watcherMatches = iElement.text().match(watcherRegExp);
if (angular.isArray(watcherMatches) && watcherMatches[2] && watcherMatches[2].length) {
scope.$watch(watcherMatches[2], updateTranslations);
}
} else {
translationIds.translate = iElement.text().replace(/^\s+|\s+$/g,'');
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ describe('pascalprecht.translate', function () {
expect(element.text()).toBe('foo');
});

it('should return newer translation if translation id exist and if its passed as interpolation', function () {
$rootScope.translationId = 'TRANSLATION_ID';
element = $compile('<div translate>{{translationId}}</div>')($rootScope);
$rootScope.$digest();
$rootScope.translationId = 'TEXT'; // refresh expression
$rootScope.$digest();
expect(element.text()).toBe('foo');
});

it('should return translation prepended by additional content when passed as interpolation', function () {
$rootScope.translationId = 'TRANSLATION_ID';
element = $compile('<div translate>abc{{translationId}}')($rootScope);
Expand Down

0 comments on commit 31c0356

Please sign in to comment.