0

For the following template:

<h2 class="viewTitle">{{viewTitle | translate}}</h2>

I'm passing sometimes a translatable string, sometimes a variable in scope. If I include the template like this:

<div ng-init="viewTitle = 'translatable.title'" ng-include="'views/templates/view-title.html'"/>

Things work. However, I cannot pass a variable from my scope. How to accomplish both?

2
  • Is your controller scope is valid with the template?Is your controller associated with the template? Commented Apr 28, 2015 at 19:34
  • if you do it from controller then you need to change you template html to <h2 class="viewTitle">{{$parent.viewTitle | translate}}</h2> because ng-include does create a child scope Commented Apr 28, 2015 at 19:38

1 Answer 1

1

your main issue is this:

How to set scope property with ng-init?. you are reading a scope attribute with ng-init before angular had time to write it. You cannot access $scope attributes in ng-init. instead set the $scope attributes in your controller (js code).

these are additional issues you will probably encounter:

this will work (see below), but you are probably encountering the typical angular scoping issues, for which there are two possible solutions:

Always follow the dot-rule (https://egghead.io/lessons/angularjs-the-dot), will help you to avoid many scoping issues in our career

or use the "controller as" syntax (http://toddmotto.com/digging-into-angulars-controller-as-syntax/).

template:

<h2 class="viewTitle">{{myScopeAttributeInParentScope | translate}}</h2> data: {{myScopeAttributeInParentScope}}  

using with String:

<div ng-init="myScopeAttributeInParentScope=translatable.title'}" ng-include="'views/templates/view-title.html'"/>

using with scope attribute:

<div ng-include="'views/templates/view-title.html'"/>  
parent Scope: {{myScopeAttributeInParentScope}} 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer and specially the links. However, the scope attribute still isn't working. Is it trying to find in the translation files an entry with the name of the given attribute maybe?
just add some debugging code, this will make your life easier (see edited answer above). see also this post, which explains the scoping issue very nicely: stackoverflow.com/a/14146317/2766511
Indeed, they are both empty I supposed, since the output is data: data.viewTitle:
Actually adding the $parent hack mentioned in the post you sent me I can show the translatable strings. The debug for the attribute (with $parent) is data: {} data.viewTitle:
also add the debugging code in your parent template, only then you see the full truth. I would expect that at the time, when your ng-init is executed, then the $scope.myScopeAttributeInParentScope is still empty, so it will not work. move your ng-init code the the controller.js.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.