I have already discussed in my last couple of blogs to interact Components using @Input and @Output. Here I am going to explain about Template Variable to interact with Child Components. By using this Template reference variable you can access public properties and methods of a child component and also to bind data to a child component.
Template Reference Variable allow you to specify a variable name on a component and you can access any public property or methods on that component using that variable.
In order to demonstrate I have a product card component which is my child component placed inside the ProductListComponent(parent component) to list some products. You can see I have added a template reference variable named #productReference on this child component
So over here in our ProductCardComponent will just a create a method and property to access from the ProductListComponent
Now you can access this property and method from the ProductListComponent
Breaking our application in to multiple components makes our application more organized and maintainable. Here I am going to explain how to pass data from parent component to child component.
Here is our ProductListComponent , which is the parent component with a productObject declared which I am going to pass in to the child component.
This is my productList html for displaying the product object. You can see I am calling the product-card component(Child Component) from this productListComponent
Child Component template
In this component I have declared a variable named product decorated with @Input , which tell to the child component that , you will get an input product from the parent component(ProductListComponent). For passing the input data from the parent component to the child component you have to add an attribute match with property decorated with the @Input inside a square bracket like below and the value here I am passing will be the productObject from the ProductListComponent
I got this error after installing the jQuery typings .The error is because of you have installed the definition files into multiple locations in your project. Both globally and locally. For get rid of this error you have to remove either of this definition files from your prooject.
npm remove @types/jquery –save-dev
Hope this helps