typescript - Angular2 pass attribute to class constructor -


how pass attributes parent component child components' class constructor in angular 2?

half of mystery figured out, attributes can passed view without problem.

/client/app.ts

import {component, view, bootstrap} 'angular2/angular2'; import {example} '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 component";     this.somehyphenatedattr = "hyphenated attribute passed component";     this.somealias = "attribute passed component aliased";   } }  bootstrap(app); 

/client/example.ts

import {component, view, attribute} 'angular2/angular2';  @component({   selector: 'example',   properties: ['test', 'hyphenatedtest', 'alias: aliastest'] }) @view({   template: `     <p>test: {{test}}</p>     <!-- result: attribute passsed component -->     <p>hyphenated: {{hyphenatedtest}}</p>     <!-- result: hyphenated attribute passed component -->     <p>aliased: {{alias}}</p>     <!-- result: attribute passed component aliased -->      <button (click)="attributecheck()">what value of 'this.test'?</button>     <!-- result: attribute passed component -->   ` }) /******************************************************************* * here problem. how access attribute inside class?  *******************************************************************/ export class example {   constructor(@attribute('test') test:string) {      console.log(this.test); // result: undefined      console.log(test); // result: null   }   attributecheck() {     alert(this.test);   } } 

to re-iterate:

how can access attribute inside child component's class passed in parent component?

repo

updated beta.1

you can use ngoninit this

@component({   selector: 'example',   inputs: ['test', 'hyphenatedtest', 'alias: aliastest'],   template: `     <p>test: {{test}}</p>     <!-- result: attribute passsed component -->     <p>hyphenated: {{hyphenatedtest}}</p>     <!-- result: hyphenated attribute passed component -->     <p>aliased: {{alias}}</p>     <!-- result: attribute passed component aliased -->      <button (click)="attributecheck()">what value of 'this.test'?</button>     <!-- result: attribute passed component -->   ` }) export class example {    ngoninit() {     console.log(this.test);     this.attributecheck();   }    attributecheck() {     alert(this.test);   } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -