2

I have two Components(Component1 and Component2) in my Angular Application. These two components I have rendered in my app component. Now I want to send a value from Component1 to Component2. How to Send it. Are there any best practices??

In Component1 I have a code Like this. Component1.html

<button (click)="passData()">Click Me</button>

Component1.ts

import { Component, OnInit , Output , EventEmitter } from '@angular/core';
@Component({
  selector: 'app-component1',
  templateUrl: './component1.component.html',
  styleUrls: ['./component1.component.css']
})
export class Component1Component implements OnInit {

   Component1Variable:string="Component1VariableSent";
  constructor() { }
  ngOnInit() {
  }
   @Output()
   SendValue=new EventEmitter<string>();
   passData()
   {
        this.SendValue.emit(this.Component1Variable);
   }
}

In Component2.html I have

<app-component1 (SendValue)="ValueFromComp1($event)"> </app-component1>
{{ValueFromComponent1}}

In Component2.ts I have

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-component2',
  templateUrl: './component2.component.html',
  styleUrls: ['./component2.component.css']
})
export class Component2Component implements OnInit {
  ValueFromComponent1:any;
  constructor() { }

  ngOnInit() {
  }
  ValueFromComp1(var1:any)
  {
      this.ValueFromComponent1=var1;
  }

}

In appcomponent.html I hav code Like this.

<app-component1></app-component1>
<app-component2></app-component2>

Now I am getting to send the value from component1 to component2. But there is a click button two times in the output.

And another question is that I want to transfer data from one Component to another Component which resides in the same hirearchy of component tree. By doing in the above way I am not getting the components in the same hirearchy of component tree.

6
  • Please add the code that demonstrates what you try to accomplish. Commented Nov 21, 2017 at 7:22
  • did you do a search on google? Commented Nov 21, 2017 at 7:23
  • Yes I did a search. Commented Nov 21, 2017 at 7:23
  • best place to start angular.io/guide/component-interaction Commented Nov 21, 2017 at 7:24
  • @roopteja you are using event emitters just take a look at it and it will be good to go Commented Nov 21, 2017 at 7:31

2 Answers 2

5

There are three ways you can accomplish the same .

Sign up to request clarification or add additional context in comments.

11 Comments

there are plenty of questions similar, why you are answering the same
might be he is a starter to SO and he needs some help to begin with he can take it up from there @Sajeetharan i have seen you answer questions like this too
haha i did but not these days .it make sense if he provides code!
i get it but if he were specific i could have commented, but he wasnt had to answer . you DV if you want to i will take it down
Sajeetharan I have attached a code and my question also.Please take a look into it and answer. Thanks In advance
|
4

If you are passing data from parent component to your child component you should use @input.

In your case you want to pass data from one component to another which are in the same level. i would suggest you to go for shared service.

Look at this answer to get more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.