epeleg
epeleg

Reputation: 10945

angular translate - passing scope for interpolation

When Using Angular-Translate you can pass in a map to be used for interpolation.

The syntax can be for example

{{ 'KEY_TO_TRANSLATE' | translate:interpulationValues }}

The problem is that this requires that I collect the variables I want to pass in on a special object.

I wanted to know it there is a way to pass the entire current scope (+ inherited) to Translate so that anything that is on the scope can be used in the interpolation ?

Thank

Upvotes: 1

Views: 2275

Answers (3)

Byron
Byron

Reputation: 771

you can use dot notation from any object:

if we have in our scope the following object durationValues and we pass it as a argument to the translate the we can use any of the properties or subproperties of the datacontext

{{max}} will be interpolated from the passed in datacontext (i.e. durationValues )

   $scope.durationValues = {
            min: $scope.minDuration,
            **max**: $scope.maxDuration
        }

and we have a view fragment as in

{{ 'PLAN_DETAILS:MAXIMUM_REQUIRED' | translate:$scope.durationValues }}

where

'PLAN_DETAILS:MAXIMUM_REQUIRED': 'The plan needs to be less than or equal to **{{max}}** weeks long.'

I hope this makes things clear

Upvotes: 0

b0nyb0y
b0nyb0y

Reputation: 1446

You can just loop through $scope using regular javascript for loop. All inherited properties will be present.

This approach is good in the sense that it allows you to do some screening of variable names (such as filtering out Angular variables that start with $ or $$, as well as filtering out function names:

$scope.varList = {};
for (var key in $scope) {
    if (key !== 'varList' && 
        key.charAt(0) !== '$' && 
        key.charAt(1) !== '$' && 
        !angular.isFunction($scope[key])) {

        $scope.varList[key] = $scope[key];
    }
}

Upvotes: 0

Jscti
Jscti

Reputation: 14440

Not sure if it's a good idea but you should be able to do this this way :

{{ 'KEY_TO_TRANSLATE' | translate:this}}

You can also use the directive in order to construct an object directly in your view (filter param need a controller based object If I remember well):

<ANY translate="TRANSLATION_ID"
translate-values="{ username: someScopeObject.username }"></ANY>

see http://angular-translate.github.io/docs/#/guide/06_variable-replacement

Upvotes: 1

Related Questions