Angular2 pass-Attribut auf die Klasse constructor

Wie gebe ich die Attribute von einem übergeordneten Komponente an Ihre untergeordneten Komponenten' Konstruktor der Klasse in Angular 2?

Hälfte des mystery herausgefunden, als Attribute übergeben werden können, um die Ansicht ohne problem.

/client/app.ts

import {Component, View, bootstrap} from 'angular2/angular2';
import {Example} from 'client/example';

@Component({
  selector: 'app'
})
@View({
  template: `<p>Hello</p>
    <example [test]="someAttr"
      [hyphenated-test]="someHyphenatedAttr"
      [alias-test]="someAlias"></example>
    `,
  directives: [Example]
})
class App {
  constructor() {
    this.someAttr = "attribute passsed to component";
    this.someHyphenatedAttr = "hyphenated attribute passed to component";
    this.someAlias = "attribute passed to component then aliased";
  }
}

bootstrap(App);

/Kunden/Beispiel.ts

import {Component, View, Attribute} from 'angular2/angular2';

@Component({
  selector: 'example',
  properties: ['test', 'hyphenatedTest', 'alias: aliasTest']
})
@View({
  template: `
    <p>Test: {{test}}</p>
    <!-- result: attribute passsed to component -->
    <p>Hyphenated: {{hyphenatedTest}}</p>
    <!-- result: hyphenated attribute passed to component -->
    <p>Aliased: {{alias}}</p>
    <!-- result: attribute passed to component then aliased -->

    <button (click)="attributeCheck()">What is the value of 'this.test'?</button>
    <!-- result: attribute passed to component -->
  `
})
/*******************************************************************
* HERE IS THE PROBLEM. How to access the attribute inside the class? 
*******************************************************************/
export class Example {
  constructor(@Attribute('test') test:string) {
     console.log(this.test); //result: undefined
     console.log(test); //result: null
  }
  attributeCheck() {
    alert(this.test);
  }
}

Zu re-iterieren:

Wie kann ich den Zugriff auf eine Attribut in der Kind-Komponente, Klasse übergeben von der übergeordneten Komponente?

Repo

InformationsquelleAutor shmck | 2015-08-05
Schreibe einen Kommentar