typescript - Access to the parent value from child component [Angular] -


appcomponent (parent) has main page (layout) , counter of pages:

export class appcomponent {   counter = '1/3'; }  <div class="counter">   {{counter}} </div> 

there 2 components (children) responsible content of pages. child components, need have access parent value (counter).

one of them:

import {appcomponent} '../app.component';  export class page1component {   app: appcomponent; } someevent($event) {   this.app.counter='2/3'; }  <div class="content">  ... </div> 

after event error: "typeerror: cannot read property 'counter' of undefined"

question: how manipulate parent variable correctly?

add input field in child component:

@input() counter: any; 

and can bind porperty in parent html:

<child-component-selector [counter]="counter"></child-component-selector> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -