How to pass variables from .ts to html


Giuseppe Vergari

I am using the Hero demo provided from the angular website. I modified it to keep the data in the rest of the server. That's ok, but when I try to open the hero's details it misses printing every detail like last name ecc...

I think there is a problem between "hero-detail.component.ts" and "hero-detail.component.html".

When I try to print this.hero.nome, it shows nothing, so I think that's an empty class.

My rest server confirmed that it sent all the information correctly.

Here is my hero-detail.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';

import { Hero }         from '../hero';
import { HeroService }  from '../hero.service';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: [ './hero-detail.component.css' ]
})
export class HeroDetailComponent implements OnInit {
  @Input() hero: Hero;



  constructor(
    private route: ActivatedRoute,
    private heroService: HeroService,
    private location: Location
  ) {}

  ngOnInit(): void {
    this.getHero();
  }

  getHero(): void {
    const id = +this.route.snapshot.paramMap.get('id');
    this.heroService.getHero(id)
    .subscribe(hero => this.hero = hero);

  }

  goBack(): void {
    this.location.back();
  }

 save(): void {
    this.heroService.updateHero(this.hero)
      .subscribe(() => this.goBack());
  }
}

and my hero-detail.component.html

<div *ngIf="hero">

  <h2>{{hero.nome | uppercase}} Dettagli</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>Nome:
      <input [(ngModel)]="hero.nome" placeholder="Nome"/>

      {{hero.nome}}
    </label>
    </div>
    <div>
    <label >Cognome:
      <input [(ngModel)]="hero.cognome" placeholder="Cognome"/>
    </label>
  </div>



  <button (click)="goBack()">go back</button>
  <button (click)="save()">save</button>
</div>

In the HTML file I want to display the first and last name of the hero provided by my rest server

Tempes

I think we have a bug here, if I support you using the Heros project from the angular page, the response from the service will be in English instead of Italian.

<div *ngIf="hero">

  <h2>{{hero.name | uppercase}} Dettagli</h2>
  <div><span>id: </span>{{hero.id}}</div>
  <div>
    <label>Nome:
      <input [(ngModel)]="hero.name" placeholder="Nome"/>

      {{hero.name}}
    </label>
    </div>
    <div>
    <label >Cognome:
      <input [(ngModel)]="hero.lastname" placeholder="Cognome"/>
    </label>
  </div>



  <button (click)="goBack()">go back</button>
  <button (click)="save()">save</button>
</div>

Then

<h2>{{hero.nome | uppercase}} Details</h2>

will

<h2>{{hero.name | uppercase}} Details</h2>

hero.nome will be hero.name and I guess user.cognome will be user.lastname.

Hope to help you! :D

Related


How to pass variables from .ts to html

Giuseppe Vergari I am using the Hero demo provided from the angular website. I modified it to keep the data in the rest of the server. That's ok, but when I try to open the hero's details it misses printing every detail like last name ecc... I think there is a

How to pass variables from .ts to html

Giuseppe Vergari I am using the Hero demo provided from the angular website. I modified it to keep the data in the rest of the server. That's ok, but when I try to open the hero's details it misses printing every detail like last name ecc... I think there is a

How to dynamically pass variables from HTML to CSS

Cuba 12 I have a question. I don't know how (and if it's possible) to pass some variables from HTML to CSS. For example, I have a CSS code: .clear { clear: both; display: block; font-size: 0; line-height: 0; width:100%; } I want to pass variable to width inst

How to dynamically pass variables from HTML to CSS

Cuba 12 I have a question. I don't know how (and if it's possible) to pass some variables from HTML to CSS. For example, I have a CSS code: .clear { clear: both; display: block; font-size: 0; line-height: 0; width:100%; } I want to pass variable to width inst

How to pass id in angular form from .html to .ts?

don't stop learning Basically, how does the title say, how do I pass the ID of the item, so when visiting the page, I just show the item, and I'm currently at the stage of getting the ID. In .html: <button routerLink='/items/{{item.id}}'>List item</button> In

How to pass value from HTML field to component ts

Hasoun GH I'm sending a value from an HTML page to a TS component without using a form, I get a build error when trying to npm run build, here is the example: I use > npm run dev or > ng build no problem, but when I build it for production it gives an error us

How to pass values from .ts/html to css/scss in Angular?

Data Collector I am building a MEAN Stack application, and I need to change the theme color of the application by user and save it in mongo, so every time the user opens the application, the color in the database is the same, can you please help me Do this. Ta

How to pass id in angular form from .html to .ts?

don't stop learning Basically, how does the title say, how do I pass the ID of the item, so when visiting the page, I just show the item, and I'm currently at the stage of getting the ID. In .html: <button routerLink='/items/{{item.id}}'>List item</button> In

How to pass id in angular form from .html to .ts?

don't stop learning Basically, how the title says, how do I pass the ID of the item, so when accessing the page, I just show the item, and I'm currently at the stage of getting the ID. In .html: <button routerLink='/items/{{item.id}}'>List item</button> In th

How to pass value from HTML field to component ts

Hasoun GH I'm sending a value from an HTML page to a TS component without using a form, I get a build error when trying to npm run build, here is the example: I use > npm run dev or > ng build no problem, but when I build it for production it gives an error us

How to pass id in angular form from .html to .ts?

don't stop learning Basically, how the title says, how do I pass the ID of the item, so when accessing the page, I just show the item, and I'm currently at the stage of getting the ID. In .html: <button routerLink='/items/{{item.id}}'>List item</button> In th

How to pass values from .ts/html to css/scss in Angular?

Data Collector I am building a MEAN Stack application, and I need to change the theme color of the application by user and save it in mongo, so every time the user opens the application, the color in the database is the same, can you please help me Do this. Ta

How to pass values from .ts/html to css/scss in Angular?

Data Collector I am building a MEAN Stack application, and I need to change the theme color of the application by user and save it in mongo, so every time the user opens the application, the color in the database is the same, can you please help me Do this. Ta

How to pass variables from html to js directly from onclick

Rem I want to send variable from html to javascript file onclick some button. Can I write it like this: onclick="xFunc(document.getElementsByName("x").value,document.getElementsByName("y").value,document.getElementsByName("z").value);"></p> Because javascript

How to pass variables from html to js directly from onclick

Rem I want to send variable from html to javascript file onclick some button. Can I write it like this: onclick="xFunc(document.getElementsByName("x").value,document.getElementsByName("y").value,document.getElementsByName("z").value);"></p> Because javascript

How to pass variables from html to js directly from onclick

Rem I want to send variable from html to javascript file onclick some button. Can I write it like this: onclick="xFunc(document.getElementsByName("x").value,document.getElementsByName("y").value,document.getElementsByName("z").value);"></p> Because javascript

Pass variable from .ts to .html in ionic and angular

username I am new to ionic 4, can get angular data from http and retrieve json data from json, in console, i can get json perfectly, how can i pass these data from .ts to .html and display them like every display home.page.ts import { Component } from '@angula

Pass variable from .ts to .html in ionic and angular

username I am new to ionic 4, can get angular data from http and retrieve json data from json, in console, i can get json perfectly, how can i pass these data from .ts to .html and display them like every display home.page.ts import { Component } from '@angula

Pass variable from .ts to .html in ionic and angular

username I am new to ionic 4, can get angular data from http and retrieve json data from json, in console, i can get json perfectly, how can i pass these data from .ts to .html and display them like every display home.page.ts import { Component } from '@angula

Pass variable from .ts to .html in ionic and angular

username I am new to ionic 4, can get angular data from http and retrieve json data from json, in console, i can get json perfectly, how can i pass these data from .ts to .html and display them like every display home.page.ts import { Component } from '@angula

Pass variables from Javascript to HTML

Piman My code registers a click event which will open a dialog and display some information. I want the dialog to also show where the user clicked on the page. Currently, the information is displayed in the alert box, but I want it to appear in the dialog. Can

Pass variables from PHP to HTML

David Hermoch I have a simple .php file with games of Rock, Paper and Scissors (with some homework) that keeps track of your score, and a file .htmlwith buttons to choose from R/P/S. The score is tracked via a session variable, is there an easy way to pass the

Pass variables from Python to html

anonymous I have a function that sends a mail to the user like this: import os import smtplib import imghdr from email.message import EmailMessage EMAIL_ADDRESS = email EMAIL_PASSWORD = password msg = EmailMessage() msg['Subject'] = 'Email Title' msg['From'

Pass variables from Javascript to HTML

Piman My code registers a click event which will open a dialog and display some information. I want the dialog to also show where the user clicked on the page. Currently, the information is displayed in the alert box, but I want it to appear in the dialog. Can

Pass variables from PHP to HTML

David Hermoch I have a simple .php file with games of Rock, Paper and Scissors (with some homework) that keeps track of your score, and a file .htmlwith buttons to choose from R/P/S. The score is tracked via a session variable, is there an easy way to pass the

Send dynamic variables from .ts to .html in angular

realist I have a grid and I want to send class conditions from a .tsfile . I achieved this with myClassCondition = 5 < 6 ? 'bg-red' : null;the code below . However, I want "col" and "rowData" like below. gives me compile errors because "col" and "rowData" are

How to pass variables from views to base.html file in Django?

Joao De Sousa: I want to tell the user's name in the navigation bar of my project. This is the file on the navigation bar .base.html In my views.pyI created a variable, does this def base(request): user = request.user u = User.objects.get(username=user

How to pass generated variables from one html file to another

wild and rare I am trying to pass a variable from an html file to another. The variable is generated as a random three-digit number. fn = Math.floor(Math.random() * 10).toString(); sn = Math.floor(Math.random() * 10).toString(); tn = Math.floor(Mat

How to pass variables from views to base.html file in Django?

Joao De Sousa: I want to tell the user's name in the navigation bar of my project. This is the file on the navigation bar .base.html In my views.pyI created a variable, does this def base(request): user = request.user u = User.objects.get(username=user