typescript Angular2 将属性传递给类构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31829254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 pass attribute to class constructor
提问by shmck
How do I pass attributes from a parent component to their child components' class constructor in Angular 2
?
如何将属性从父组件传递到其子组件的类构造函数中Angular 2
?
Half of the mystery is figured out, as attributes can be passed to the view without problem.
一半的谜团已经解开,因为属性可以毫无问题地传递给视图。
/client/app.ts
/客户端/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);
/client/example.ts
/客户端/example.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);
}
}
To re-iterate:
重新迭代:
How can I access an attribute inside the child component's classpassed in from the parent component?
如何访问从父组件传入的子组件类中的属性?
回答by Eric Martinez
Updated to beta.1
更新到 beta.1
You can use ngOnInitfor this
您可以为此使用ngOnInit
@Component({
selector: 'example',
inputs: ['test', 'hyphenatedTest', 'alias: aliasTest'],
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 -->
`
})
export class Example {
ngOnInit() {
console.log(this.test);
this.attributeCheck();
}
attributeCheck() {
alert(this.test);
}
}
回答by haz111
If you want to have access to properties values in child component you can:
如果您想访问子组件中的属性值,您可以:
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 -->
`
})
export class Example {
_test: string;
_hyphenatedTest: any; //change to proper type
_alias: any; //change to proper type
constructor() {
}
set test(test) {
this._test = test;
}
set hyphenatedTest(hyphenatedTest) {
this._hyphenatedTest = hyphenatedTest;
}
set alias(alias) {
this._alias = alias;
}
}
set
method is run when the value of property changes. Value is passed to your local variable on which you can operate in component.
set
方法在属性值更改时运行。值传递给您可以在组件中操作的局部变量。
Important things:
重要的事:
- set method is always executed after constructor
- I don't know why but the name of parameter in
set
method must be same as name of this method (so it means it must be the same as name of property)
- set 方法总是在构造函数之后执行
- 我不知道为什么但是
set
方法中的参数名称必须与此方法的名称相同(因此它意味着它必须与属性名称相同)