SlideShare a Scribd company logo
1 of 14
Petal Diagram Template
Databinding with Component
(ways to pass data into
Components)
Angular
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components:
Angular
@Input
@Input is a decorator to mark a property as an input. @Input is used to define
an input property, to achieve component property binding. @Input decorator
is used to pass data (property binding) from parent to child component. The
component property should be annotated with @Input decorator to act as
input property.
@Output
@Output decorator is used to pass the data from child to parent component.
@Output decorator binds a property of a component, to send data from one
component to the calling component. @Output binds a property of the type of
angular EventEmitter class.
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components:
Angular
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components: @Input()
Angular
child.component.ts
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<h4><b>Child Component</b></h4>
<h5>My name is <b>{{name}}</b></h5>
`
})
export class ChildComponent implements OnInit {
@Input() name: string;
constructor() { }
ngOnInit(): void {
}
}
parent.component.html
<div>
<h3><b>Parent Component</b></h3>
<app-child [name]="'Mohit'"></app-child>
</div>
This is the data sending
to child component from
parent component
String interpolation to display name that is coming
from parent component
@Input Decorator to pass data from parent to child
component
Denotes those variables which
will be used by child component
with @Input decorator to fetch
the message from parent
component
(@input property name)
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components: @Output()
Angular
child.component.ts
import { Component, EventEmitter, Output } from
'@angular/core';
@Component({
selector: 'app-child',
template: `<h4><b>Child Component</b></h4>
<button (click)="send()"><b>Click</b></button>`
})
export class ChildComponent {
@Output() username = new EventEmitter<string>();
send() {
this.username.emit('Moh');
}
}
EventEmitter is a module that helps share data between
components using emit() and subscribe() methods.
EventEmitter is in the Observables layer, which
observes changes and values and emits the data to the
components subscribed to that EventEmitter instance.
(click) is event that calling a send function in child ts file
Adding a property with @Output decorator which has a
type of EventEmitter
send() is function that is sending data to parent component
using emit function.
EventEmitter is used with @Output directive to emit custom
events asynchronously and synchronously, and register
handlers for those events by subscribing to an instance.
Mohit Upadhyay
Ways to pass data into Components: @Output()
Angular
parent.component.html
<h3>Parent Component</h3>
<app-child (username)="show($event)"></app-child>
<h3><b>{{username}}</b></h3>
parent.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent {
username:string;
show(username:string) {
this.username = username;
}
}
(username) is a child component event
show($event) is a function calling when that event is
triggered
display the data that come from child component using
string interpolation
show() function is assigning data to local variable of
parent file
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components:
Angular
@viewChild
@viewChild decorator help to access a directive, child component and the
DOM element from a component class. The @ViewChild decorator returns the
element that match reference selector for defined directive, template or
component. Using @ViewChild decorator, you can call a child component's
method any time after the child component has got loaded. You can access the
child component 's properties or method or after the "AfterViewInit" method of
the parent component.
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components: @viewChild()
Angular
parent.component.ts
import { Component, ViewChild, AfterViewInit } from
'@angular/core';
import { Child1Component } from './child/child.component';
@Component({
selector: 'app-view-child',
template:`<p>parent-component!</p>
{{message}}
<app-child1 #child></app-child1>`
})
export class ViewChildComponent implements AfterViewInit
{
@ViewChild('child') private child: Child1Component;
message: string = '';
ngAfterViewInit() {
this.message = this.child.getMessage();
}
}
For ViewChild() to work
Import ViewChild() and ngAfterViewInit() and
implement the lifecycle hook
Import the child component.
Use the @ViewChild() directive.
Declare a variable that holds the data.
In ngAfterViewInit() call the function that holds the
data.
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components: @viewChild()
Angular
child.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-child1',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class Child1Component {
getMessage() {
console.log("child message");
return "Child component message"
}
}
This is the function we are
calling from parent component
Mohit Upadhyay
Petal Diagram Template
Ways to pass data into Components:
Angular
Angular services are objects that get instantiated just once during the lifetime of an
application. They contain methods that maintain data throughout the life of an
application, i.e., data is available all the time.
The main objective of a service is to organize and share business logic, models, or
data and functions with different components of an Angular application. They are
usually implemented through dependency injection.
Angular Services :
Mohit Upadhyay
Ways to pass data into Components: Services
Angular
Angular Services :
test.service.ts
export class TestService {
getData() {
return [
{ id: 100, name: 'Mahak', age: 22, gender: 'Female', email:
'mahak@gmail.com', phoneNo: 998989898 },
{ id: 101, name: 'cheerag', age: 22, gender: 'Male', email:
'cheerag@gmail.com', phoneNo: 6565656565 },
{ id: 102, name: 'Mohit', age: 22, gender: 'Male', email:
'mohit@gmail.com', phoneNo: 7877878787 }
];
}
}
test1.service.ts
import { Injectable } from '@angular/core';
import { TestService } from './test.service';
@Injectable()
export class Test1Service {
constructor(private testService:TestService) { }
message:string = "This data coming from test1 service file";
getUserInfo() {
return [{info: this.testService.getData(),msg:this.message}];
}
}
Mohit Upadhyay
Ways to pass data into Components: Services
Angular
test.component.ts
import { Component, OnInit } from '@angular/core';
import { TestService } from '../test.service';
@Component({
selector: 'app-testexaple1',
template: `<p>testexaple1 works!</p>
<div *ngFor="let info of userInfo">
{{info.id}}|{{info.name}}|{{info.age}}|{{info.gender}}|{{info.email}}|{{info.phoneNo}}
</div>`
})
export class Testexaple1Component implements OnInit {
userInfo;
constructor(private testService: TestService) { }
ngOnInit(){
this.userInfo = this.testService.getData();
}
} Mohit Upadhyay
Ways to pass data into Components: Services
Angular
test1.component.ts
import { Component, OnInit } from '@angular/core';
import { Test1Service } from '../test1.service';
@Component({
selector: 'app-testexaple2',
template:`<p>testexaple1 works!</p>
<div *ngFor="let userInfo of information">
<div *ngFor="let info of userInfo.info">
{{info.id}}|{{info.name}}
</div>
{{userInfo.msg}}
</div>`,
providers: [Test1Service]
})
export class Testexaple2Component implements OnInit
{
information;
constructor(private test1Service: Test1Service) { }
ngOnInit(): void {
console.log(this.test1Service.getUserInfo());
this.information = this.test1Service.getUserInfo();
}
}
Mohit Upadhyay
Thankyou
Mohit Upadhyay

More Related Content

Similar to passDataB_wComponentInAngular.pptx

Similar to passDataB_wComponentInAngular.pptx (20)

Angular 2 binding
Angular 2  bindingAngular 2  binding
Angular 2 binding
 
Data Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJSData Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJS
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java Developers
 
angular-concepts-introduction-slides.pptx
angular-concepts-introduction-slides.pptxangular-concepts-introduction-slides.pptx
angular-concepts-introduction-slides.pptx
 
Building a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web ServicesBuilding a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web Services
 
Angular 2 in-1
Angular 2 in-1 Angular 2 in-1
Angular 2 in-1
 
React render props
React render propsReact render props
React render props
 
Angular resolver tutorial
Angular resolver tutorialAngular resolver tutorial
Angular resolver tutorial
 
Interoperable Component Patterns
Interoperable Component PatternsInteroperable Component Patterns
Interoperable Component Patterns
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
 
Data Flow Patterns in Angular 2 - Sebastian Müller
Data Flow Patterns in Angular 2 -  Sebastian MüllerData Flow Patterns in Angular 2 -  Sebastian Müller
Data Flow Patterns in Angular 2 - Sebastian Müller
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
 
What is data binding in Angular
What is data binding in AngularWhat is data binding in Angular
What is data binding in Angular
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
 
Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1Exploring Angular 2 - Episode 1
Exploring Angular 2 - Episode 1
 
Asp.NET MVC
Asp.NET MVCAsp.NET MVC
Asp.NET MVC
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 

passDataB_wComponentInAngular.pptx

  • 1. Petal Diagram Template Databinding with Component (ways to pass data into Components) Angular Mohit Upadhyay
  • 2. Petal Diagram Template Ways to pass data into Components: Angular @Input @Input is a decorator to mark a property as an input. @Input is used to define an input property, to achieve component property binding. @Input decorator is used to pass data (property binding) from parent to child component. The component property should be annotated with @Input decorator to act as input property. @Output @Output decorator is used to pass the data from child to parent component. @Output decorator binds a property of a component, to send data from one component to the calling component. @Output binds a property of the type of angular EventEmitter class. Mohit Upadhyay
  • 3. Petal Diagram Template Ways to pass data into Components: Angular Mohit Upadhyay
  • 4. Petal Diagram Template Ways to pass data into Components: @Input() Angular child.component.ts import { Component, Input, OnInit } from '@angular/core'; @Component({ selector: 'app-child', template: ` <h4><b>Child Component</b></h4> <h5>My name is <b>{{name}}</b></h5> ` }) export class ChildComponent implements OnInit { @Input() name: string; constructor() { } ngOnInit(): void { } } parent.component.html <div> <h3><b>Parent Component</b></h3> <app-child [name]="'Mohit'"></app-child> </div> This is the data sending to child component from parent component String interpolation to display name that is coming from parent component @Input Decorator to pass data from parent to child component Denotes those variables which will be used by child component with @Input decorator to fetch the message from parent component (@input property name) Mohit Upadhyay
  • 5. Petal Diagram Template Ways to pass data into Components: @Output() Angular child.component.ts import { Component, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-child', template: `<h4><b>Child Component</b></h4> <button (click)="send()"><b>Click</b></button>` }) export class ChildComponent { @Output() username = new EventEmitter<string>(); send() { this.username.emit('Moh'); } } EventEmitter is a module that helps share data between components using emit() and subscribe() methods. EventEmitter is in the Observables layer, which observes changes and values and emits the data to the components subscribed to that EventEmitter instance. (click) is event that calling a send function in child ts file Adding a property with @Output decorator which has a type of EventEmitter send() is function that is sending data to parent component using emit function. EventEmitter is used with @Output directive to emit custom events asynchronously and synchronously, and register handlers for those events by subscribing to an instance. Mohit Upadhyay
  • 6. Ways to pass data into Components: @Output() Angular parent.component.html <h3>Parent Component</h3> <app-child (username)="show($event)"></app-child> <h3><b>{{username}}</b></h3> parent.component.ts import { Component } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent { username:string; show(username:string) { this.username = username; } } (username) is a child component event show($event) is a function calling when that event is triggered display the data that come from child component using string interpolation show() function is assigning data to local variable of parent file Mohit Upadhyay
  • 7. Petal Diagram Template Ways to pass data into Components: Angular @viewChild @viewChild decorator help to access a directive, child component and the DOM element from a component class. The @ViewChild decorator returns the element that match reference selector for defined directive, template or component. Using @ViewChild decorator, you can call a child component's method any time after the child component has got loaded. You can access the child component 's properties or method or after the "AfterViewInit" method of the parent component. Mohit Upadhyay
  • 8. Petal Diagram Template Ways to pass data into Components: @viewChild() Angular parent.component.ts import { Component, ViewChild, AfterViewInit } from '@angular/core'; import { Child1Component } from './child/child.component'; @Component({ selector: 'app-view-child', template:`<p>parent-component!</p> {{message}} <app-child1 #child></app-child1>` }) export class ViewChildComponent implements AfterViewInit { @ViewChild('child') private child: Child1Component; message: string = ''; ngAfterViewInit() { this.message = this.child.getMessage(); } } For ViewChild() to work Import ViewChild() and ngAfterViewInit() and implement the lifecycle hook Import the child component. Use the @ViewChild() directive. Declare a variable that holds the data. In ngAfterViewInit() call the function that holds the data. Mohit Upadhyay
  • 9. Petal Diagram Template Ways to pass data into Components: @viewChild() Angular child.component.ts import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-child1', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class Child1Component { getMessage() { console.log("child message"); return "Child component message" } } This is the function we are calling from parent component Mohit Upadhyay
  • 10. Petal Diagram Template Ways to pass data into Components: Angular Angular services are objects that get instantiated just once during the lifetime of an application. They contain methods that maintain data throughout the life of an application, i.e., data is available all the time. The main objective of a service is to organize and share business logic, models, or data and functions with different components of an Angular application. They are usually implemented through dependency injection. Angular Services : Mohit Upadhyay
  • 11. Ways to pass data into Components: Services Angular Angular Services : test.service.ts export class TestService { getData() { return [ { id: 100, name: 'Mahak', age: 22, gender: 'Female', email: 'mahak@gmail.com', phoneNo: 998989898 }, { id: 101, name: 'cheerag', age: 22, gender: 'Male', email: 'cheerag@gmail.com', phoneNo: 6565656565 }, { id: 102, name: 'Mohit', age: 22, gender: 'Male', email: 'mohit@gmail.com', phoneNo: 7877878787 } ]; } } test1.service.ts import { Injectable } from '@angular/core'; import { TestService } from './test.service'; @Injectable() export class Test1Service { constructor(private testService:TestService) { } message:string = "This data coming from test1 service file"; getUserInfo() { return [{info: this.testService.getData(),msg:this.message}]; } } Mohit Upadhyay
  • 12. Ways to pass data into Components: Services Angular test.component.ts import { Component, OnInit } from '@angular/core'; import { TestService } from '../test.service'; @Component({ selector: 'app-testexaple1', template: `<p>testexaple1 works!</p> <div *ngFor="let info of userInfo"> {{info.id}}|{{info.name}}|{{info.age}}|{{info.gender}}|{{info.email}}|{{info.phoneNo}} </div>` }) export class Testexaple1Component implements OnInit { userInfo; constructor(private testService: TestService) { } ngOnInit(){ this.userInfo = this.testService.getData(); } } Mohit Upadhyay
  • 13. Ways to pass data into Components: Services Angular test1.component.ts import { Component, OnInit } from '@angular/core'; import { Test1Service } from '../test1.service'; @Component({ selector: 'app-testexaple2', template:`<p>testexaple1 works!</p> <div *ngFor="let userInfo of information"> <div *ngFor="let info of userInfo.info"> {{info.id}}|{{info.name}} </div> {{userInfo.msg}} </div>`, providers: [Test1Service] }) export class Testexaple2Component implements OnInit { information; constructor(private test1Service: Test1Service) { } ngOnInit(): void { console.log(this.test1Service.getUserInfo()); this.information = this.test1Service.getUserInfo(); } } Mohit Upadhyay