1

main.component.html

<form (submit)="onSubmit()">
    height:
    <input type="number" name="height" [(ngModel)]="user.height">
    <br><br>
    Weight:
    <input type="number" name="weight"[(ngModel)]="user.weight">
    <br><br>
    <input type="submit">
</form>

<h5 *ngFor="let user of users">

    {{user.score}}

</h5>

main.component.ts

export class MainComponent implements OnInit {
    users = [];

    user = {
        height: 0,
        weight: 0,
    };

    onSubmit() {
        console.log(this.user.height);
        console.log(this.user.weight);

        var score = this.user.weight * this.user.height;
        this.users.push(score);

        console.log(score);


        this.user = {
            height: 0,
            weight: 0
        }
    }
}

this.user.weight and this.user.height are multiplied and saved in var score. But how do I pass the var score to show in my main.component.html? I tried to push score in users array but its not showing up in my main.component.html.

2
  • you don't have a member score in user. add it and store the calculated value to it. Commented Sep 20, 2017 at 10:15
  • You're using TypeScript, but you'rte not using types anywhere. Specify the types of your variables, and the compiler will tell you what you're doing wrong. users is supposed (I guess) to be an array of users (so Array<User>), but you're pushing scores to this array (of type number). You're supposed to display the score of each user, but you don't have any score in the users. Also, your code is not indented, making it very hard to read. This is all basic stuff. Learn and apply the basics. Commented Sep 20, 2017 at 10:18

3 Answers 3

2

Don't push it into users; remove var and use this.score = this.user.weight * this.user.height, and then in Your main...html you can use as {{score}}

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

Comments

1

score is not a object , it should be

<div *ngFor="let user of users">
<h1>{{user }}</h1>
</div>

3 Comments

user is an object. users is an array. Don't miss the s
@i-- no: Read the code: it populates the array using var score = this.user.weight * this.user.height; this.users.push(score);. So the elemtns of the array are numbers.
@i-- actually score
0

Try this :

<h5 *ngFor="let user of users">
{{user | json}}
</h5>

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.