Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  195] [ 5]  / answers: 1 / hits: 31527  / 6 Years ago, sat, november 3, 2018, 12:00:00

I come from a Python background, but I started trying to learn Angular and I'm really having trouble. Working between components is confusing to me and I can't figure it out. I made a good example that I think if someone helped me with it would go along way towards understanding Angular.



I just have two components: a header component and an app component. In the header component, I ask for the user's name and they click a button, and then it should show Hello {{name}} in the next component. I cannot get it to work to say the least and it's really frustrating. The Header part seems to work okay, but it's just not communicating with the other component at all. Neither the button part or the name part are working so I am clearly misunderstanding something I need to do when it comes to listening from the parent component.



Here is my Header HTML:



Name: <input type=text id=userInput value=Joe>

<button (click)=showName()>Show More</button>


Here is my Header TS:



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

@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
bodyDiv = false;
inputName = '';
@Output() buttonClicked = new EventEmitter();

constructor() { }

ngOnInit() {
}
showName() {
console.log('showName clicked.');
this.bodyDiv = true;
this.inputName = document.getElementById('userInput').value;
console.log(this.inputName);
console.log(this.bodyDiv);
this.buttonClicked.emit(this.bodyDiv);
this.buttonClicked.emit(this.inputName);
}
}


Here is the main Component's HTML:



<app-header (buttonClicked)='showNextComponent($event)'></app-header>

<p *ngIf=![hiddenDiv] [inputName]=name>Hello {{ name }} </p>


Here is the main component's TS:



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

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
hiddenComponent = true;
title = 'show-button';

showNextComponent() {
console.log('Button clicked.');
this.hiddenComponent = false;
console.log(this.hiddenComponent);
}
}


So who can show me what I'm doing wrong and help figure out Angular a little better? :) Thank you!


More From » html

 Answers
102

replace showName function with below code :





showName() {
console.log('showName clicked.');
this.bodyDiv = true;
this.inputName = document.getElementById('userInput').value;
console.log(this.inputName);
console.log(this.bodyDiv);
this.buttonClicked.emit(this.inputName);
}




replace below code in your main component.



name:string
showNextComponent(value:string) {
this.name = value;
}




replace below code in your html :





<app-header (buttonClicked)='showNextComponent($event)'></app-header>

<p *ngIf=name>Hello {{ name }} </p>





Please let me if you have any question and I would suggest try to use ngmodel or something else instead of directly communicating with the DOM.


[#53183] Tuesday, October 30, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
draven

Total Points: 641
Total Questions: 101
Total Answers: 110

Location: Nicaragua
Member since Mon, Sep 26, 2022
2 Years ago
draven questions
Tue, Apr 12, 22, 00:00, 2 Years ago
Thu, Feb 17, 22, 00:00, 2 Years ago
Tue, Nov 30, 21, 00:00, 3 Years ago
Tue, Oct 19, 21, 00:00, 3 Years ago
;