Issue
I have two components like these:
@Component({
selector: 'comp1',
template: `<h1>{{ custom_text }}</h2>`
})
export class Comp1 {
custom_text:string;
constructor(text:string) {
this.custom_text = text;
}
}
/*********************************************/
@Component({
selector: 'comp2',
directives: [Comp1],
template: `
<b>Comp 2</b>
<comp1></comp1>
`
})
export class Comp2 {
constructor() {
// ...
// How to send my own text to comp1 from comp2
// ...
}
}
Is it possible to send my own text from comp1
to comp2
?
Is it possible to get the comp1
instance from comp2
?
Solution
Yes it is very easy to accomplish that,
Checkout the Tutorial : MULTIPLE COMPONENTS Part 3 of the Angular2 Tutorials to see how to send inputs.
@Component({
selector: 'comp1',
template: `<h1>{{customText}}</h2>`,
inputs: ['customText']
})
export class Comp1 {
public customText:string;
constructor(text:string) {
this.customText= text;
}
// ngOnChange to make sure the component is in sync with inputs changes in parent
ngOnChanges() {
this.customText= text;
}
}
/*********************************************/
@Component({
selector: 'comp2',
directives: [Comp1],
template: `
<b>Comp 2</b>
<comp1 customText = "My Custom Test"></comp1>
`
})
export class Comp2 {
constructor() {
}
}
Try it out and let me know how it went.
Answered By - C B
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.