1

This is part of my template:

<!-- This is the one I want! -->
<label>{{l10n('hallo') | translate}}</label>

If my Component has a function l10n its no problem to call the l10n-function

<label>{{cmp.l10n('hallo') | translate}}</label>

But that's not what I want - if possible I want l10n(<string>), somehow a global function for this template...

In AngularJS to achieve something linke this they add a function to the Controller-Scope: https://stackoverflow.com/a/12466994/504184

My problem here is that I don't have a Controller - I'm in a Component...
Second thing is that the Scope in AngularDart is quite different compared to the Scope in AngularJS...

Community
  • 1
  • 1
Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62

2 Answers2

2

You can inject the Scope in your Component and add the function.

@Component(selector: 'my-comp')
class Comp {
  Comp(Scope scope) {
    scope.context['l10n'] = (str) => "Boo[$str]";
  }
}

However, we are thinking about removing this feature in an upcoming version of AngularDart.

James deBoer
  • 2,477
  • 15
  • 17
  • Cool!!!!! Now my works like a charm. Thanks. Please!!!! don't remove this feature or if so replace it by something better :-) In this case better would be to have the l10n-function (my userdefined function) in something like an AppController - so that it is available for the whole WebApp. Would be cool + continue with your great work! – Mike Mitterer Aug 18 '14 at 14:08
  • I think it's since Angular 0.14 but now gives me: -- start -- Observer reaction functions should not change model. These watch changes were detected: These observe changes were detected: l10n("Description:"): -- end -- Here is my l10n-Function: http://goo.gl/u1FOph. It does not modify the model - it just return an L10N-object that should be handled by translate. Any ideas? – Mike Mitterer Oct 10 '14 at 11:00
  • OK - It's Angular 0.14. I downgraded to 0.13. The same - but it worked until a few days ago???? – Mike Mitterer Oct 10 '14 at 11:19
  • If I use a const-constructor ( scope.context['l10n'] = (final str) => const L10N("Hallo");) the error message disappears but this is not really a practical solution for this case. – Mike Mitterer Oct 10 '14 at 13:37
1

James answer was right before Angular 1.0 - now its even simpler.

class MyComponent {
    ...
    L10N tr(final String text) {
      return new L10N(text);
      }
}
<label class="medium">{{tr('Projectname:') | translate}}</label>

Thats it!

Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62