Mike Mitterer
Mike Mitterer

Reputation: 7180

How can I call a function in a template based on a component?

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...

Upvotes: 1

Views: 388

Answers (2)

Mike Mitterer
Mike Mitterer

Reputation: 7180

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!

Upvotes: 1

James deBoer
James deBoer

Reputation: 2495

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.

Upvotes: 2

Related Questions