1
I’m using Angularjs to create a site Multi-lingual, but I’m having problems with the elements created dynamically.
var app = angular.module('myApp', ['pascalprecht.translate']);
app.config(function($translateProvider) {
$translateProvider.translations('en', {
'title': 'title'
}.translations('pt', {
'title': 'titulo'
}
$translateProvider.preferredLanguage('en');
});
<div ng-app="myApp" id="myDiv">
<span>{{ 'title' | translate }}</span>
</div>
But if I add a new one span
to my div, with for example:
$("#myDiv").append("<span>{{ 'title' | translate }}</span>");
The content of span
is not translated with the value of title
.
$()
just add content to the DOM. In order for the content to be interpreted by the angular you need to pass it by$compile
, adding it to the scope. Example: https://stackoverflow.com/questions/18157305/compiling-dynamic-html-strings-from-database– OnoSendai
So every time I go to add a new element I must pass it by the Compile?
– Leo Letto
Yes. Internally that’s what Angularjs does with all objects under one
scope
specific. The solution on the link I passed to you inplementates the compilation as a directive.– OnoSendai