Property 'Foods' is private and only accessible Within class 'Mainlifecyclecomponent'

Asked

Viewed 60 times

0

How to access private attributes within html? I created private food, but I can’t access it in html because it’s private. has some way to access?

main-Lifecycle.ts

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


@Component({
  selector: 'app-main-lifecycle',
  templateUrl: './main-lifecycle.component.html',
  styleUrls: ['./main-lifecycle.component.css']
})
export class MainLifecycleComponent implements OnInit {
  private clients: Client[] = [];
  private foods : string [] = ["Rice", "Beans", "Pizza"];
  private name: string = "";
  private age: number;
  private food: string;

  constructor() { }

  ngOnInit(): void {
  }

}

main-Lifecycle.html

<mat-card>
    <mat-card-title>
      Clients
    </mat-card-title>
    <mat-card-content>
        <section>
            <mat-form-field class="form-field" style="width: 400px; padding-right: 20px;" >
                <input matInput placeholder="Name" [(ngModel)]="name" name="name">
            </mat-form-field>

            <mat-form-field class="form-field" style="width: 100px; padding-right: 20px" >
                <input matInput placeholder="Age" [(ngModel)]="age" name="age">
            </mat-form-field>

            <mat-form-field >
                <mat-select placeholder="Favorite Food" [(ngModel)]="food" name = "food ">
                    <mat-option *ngFor="let foodOption of foods" [value]="foodOption">
                        {{foodOption}}
                    </mat-option>
                </mat-select>
            </mat-form-field>

            <button mat-icon-button color="primary" >
                <mat-icon>check</mat-icon>
            </button>

            <button mat-icon-button color="primary" >
                <mat-icon>refresh</mat-icon>
            </button>
        </section>
    </mat-card-content>
</mat-card>  
  • Yes, when you insert private into an attribute in the class, you cannot use it anywhere but in the class itself. I just don’t understand the private question!

  • Neither did I, in fact. the teacher who put it that way, but somehow his was and mine wasn’t. he was able to access the html this way, but here in mine...

  • That’s right, you can’t use it elsewhere if you’re thinking about pure object orientation, like in Java and C#. In Javascript (Typescript) this is actually not real, because it can declare a private attribute and use it in HTML, it can access the property normally, my IDE for example points out the error: Private field Foods cannot be resolved when using the AOT Compiler., this is a Warning from the IDE, but I can see the value of the Foods property on the screen. Where you pointed out this error: Property 'Foods' is private and only accessible Within class 'Mainlifecyclecomponent'?

  • he pointed out in pro Foods. the array that is loading some foods

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.