Clicking on the image in Angular does not result in the comments being displayed as expected

I find it incredibly frustrating that the code snippet below is not working as intended. This particular piece of code was directly copied and pasted from an online Angular course I am currently taking. The objective of this code is to display a card view with accompanying comments when a picture is clicked, but unfortunately nothing happens when the picture is clicked. It's quite perplexing, especially considering there are no apparent errors present.

As you can observe in the code, there exists a "shared" folder where all the required information is stored. If necessary, I can provide the contents of this folder for further analysis.

menu.component.ts:

import { Component, OnInit } from '@angular/core';
import { Dish } from '../shared/dish'
import { DISHES } from '../shared/dishes'

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss']
})

export class MenuComponent implements OnInit {

  dishes: Dish[] = DISHES;

  selectedDish : Dish;

  onSelect( dish : Dish ) {
    this.selectedDish = dish;
  }

  constructor() { }

  ngOnInit() {
  }

}

dishdetail.component.ts:

import { Component, OnInit, Input } from '@angular/core';
import { Dish } from '../shared/dish'

@Component({
  selector: 'app-dishdetail',
  templateUrl: './dishdetail.component.html',
  styleUrls: ['./dishdetail.component.scss']
})
export class DishDetailComponent implements OnInit {

  @Input()
  dish: Dish

  constructor() { }

  ngOnInit() {
  }

}

dishdetail.component.html:

<div class="container"
    fxLayout="row"
    fxLayout.sm="column"
    fxLayout.xs="column"
    fxLayoutAlign.gt-md="space-around center"
    fxLayoutGap="10px" 
    fxLayoutGap.xs="0">

    <div fxFlex="40" *ngIf="dish">
      <mat-card>
        <mat-card-header>
          <mat-card-title>
            <h3>{{dish.name | uppercase}}</h3>
          </mat-card-title>
        </mat-card-header>
        <img mat-card-image src={{dish.image}} alt={{dish.name}}>
        <mat-card-content>
          <p>{{dish.description}}
          </p>
        </mat-card-content>
        <mat-card-actions>
          <button mat-button>LIKE</button>
          <button mat-button>SHARE</button>
        </mat-card-actions>
      </mat-card>

    </div>

    <div fxFlex="40" *ngIf="dish">
        <mat-list>
          <h3>Comments</h3>
            <mat-list-item *ngFor="let comment of dish.comments">
              <style> .mat-list-item {
                  min-height: 80px;
              }</style>
              <p matline>
              <span>{{comment.comment}}<br></span>
              <span>{{comment.rating}} Stars<br></span>
              <span>-- {{comment.author}} {{comment.date | date}}</span>
              </p>
          </mat-list-item>
        </mat-list>
    </div>

</div>

menu.component.html:

<div class="container"
     fxLayout="column"
     fxLayoutGap="10px">

  <div fxFlex>
    <div>
      <h3>Menu</h3>
      <hr>
    </div>
  </div>

  <div fxFlex>
    <mat-grid-list cols="2" rowHeight="200px">
      <mat-grid-tile *ngFor="let dish of dishes" (click) = "onSelect(dish)">
        <img height="200px" src={{dish.image}} alt={{dish.name}}>
        <mat-grid-tile-footer>
          <h1>{{dish.name | uppercase}}</h1>
        </mat-grid-tile-footer>
      </mat-grid-tile>
    </mat-grid-list>
  </div>

  <app-dishdetail [dish] = "SelectedDish"></app-dishdetail>

</div>

Answer №1

Here's a small detail that caught my eye:

selectedDish : Dish;

Followed by this:

<app-dishdetail [dish] = "SelectedDish"></app-dishdetail>

Remember, Angular is case sensitive. So it should actually be:

<app-dishdetail [dish] = "selectedDish"></app-dishdetail>

Make sure to use a lowercase "s"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Differences Between Angular Module and Library

Exciting news - Angular has recently launched version 6 with a new CLI feature that allows you to generate libraries, which they are calling a "highly anticipated feature". From a business standpoint, I am left pondering the following questions: What is ...

Enable the generation of scss.d.ts files with Next.js

I'm currently working on a project that requires the generation of .d.ts files for the scss it produces. Instead of manually creating these files, I have integrated css-modules-typescript-loader with Storybook to automate this process. However, I am ...

Is it possible for changes made to an object in a child component to be reflected in the parent component

When passing an object to the child component using , how can we ensure that changes made to a specific property in the object within the child component are visible in the parent component? In my understanding, changes would not be reflected if we were p ...

A method for performing precise division on numbers in JavaScript, allowing for a specific precision level (such as 28 decimal places)

I've searched extensively for a library that can handle division operations with more than 19 decimal places, but to no avail. Despite trying popular libraries like exact-math, decimal.js, and bignumber.js, I have not found a solution. How would you ...

When a TypeScript merged declaration composition is used with an extended target class, it fails to work properly

I have a TypeScript problem where I need to combine a class that has been extended with a few others. While searching for solutions, I came across an article outlining a pattern that I thought could be helpful: https://www.typescriptlang.org/docs/handbook ...

Using TypeScript to chain observables in a service and then subscribing to them in the component at the end

Working with Platform - Angualar 2 + TypeScript + angularFire2 Within my user.service.ts file, I have implemented the following code to initiate an initial request to a firebase endpoint in order to fetch some path information. Subsequently, I aim to util ...

Tips for using Firebase Parameterized configuration with Typescript when setting the region() on a Firebase Function

Based on this documentation, I am attempting to utilize a Firebase Parameterized configuration directly within the region() config for a function. My .env file looks like this: LOCATION = 'australia-southeast1'; And my config file is structured ...

Error encountered in React component: TypeScript TS2339 states that the property 'xyz' is not found on type 'IntrinsicAttributes...'

I am attempting to develop a straightforward React component that can accept any properties. The syntax below using any is causing issues (unexpected token just before <): export class ValidatedInput extends React.Component<any, any> {...} The p ...

Is there a way to access a specific argument in yargs using typescript?

The idea behind using yargs is quite appealing. const argv = yargs.options({ env: { alias: 'e', choices: ['dev', 'prod'] as const, demandOption: true, description: 'app environment&apos ...

Trouble with displaying ChartsJS Legend in Angular11

Despite thoroughly researching various documentation and Stack Overflow posts on the topic, I'm still encountering an odd issue. The problem is that the Legend for ChartsJS (the regular JavaScript library, not the Angular-specific one) isn't appe ...

Exploring Angular 8: Maintaining component state while navigating between components

I'm currently working on developing a reusable search page for a specific scenario: Each employee has an attribute called Role assigned to them. There is a separate page dedicated to searching and displaying available Roles. After running a search, ...

Difficulty with Angular update: TS2339 error stating that a property is not recognized on a imported object within a JSON

Recently, I encountered an issue while upgrading Angular from version 7 to 16. The import of objects from a JSON file was functioning properly in Angular 7 but is now causing errors. Specifically, it throws the error message "TS2339: property does not ex ...

Angular - Highlight a section of a string variable

Is there a way to apply bold formatting to part of a string assigned to a variable? I attempted the following: boldTxt = 'bold' message = 'this text should be ' + this.boldTxt.toUpperCase().bold() ; However, the HTML output is: thi ...

Prevent display of host attributes in Angular 2

My Angular component features a title property. // Here is the code snippet for the component: @Input('title') public title: string // This is how you can use the component in your code: <dialog [title]="bar"></foo> However, when t ...

The data type 'unknown' cannot be assigned to the type 'any[]', 'Iterable<any>', or (Iterable<any> & any[])

I have been working on creating a custom search filter in my Angular project, and it was functioning properly. However, I encountered an error in my Visual Studio Code. In my previous project, everything was working fine until I updated my CLI, which resul ...

Exploring ways to retrieve the chosen value from a personalized dropdown menu in React?

I'm currently utilizing styled components along with typescript to retrieve the selected option value of a customized dropdown. However, I am encountering an issue where the value does not update as expected. Interestingly, when I remove the opacity f ...

When using the `const { }` syntax, which attribute is made accessible to the external

I am using the ngrx store as a reference by following this example: https://stackblitz.com/edit/angular-multiple-entities-in-same-state?file=src%2Fapp%2Fstate%2Freducers%2Fexample.reducer.ts Within the code in example.reducer.ts, there is this snippet: ...

After the transition to Angular 8, the functionality of testing with Jest seems to have

After upgrading our Angular version from 7 to 8, we encountered some issues when using Jest as our test runner. Our main objective now is to ensure that our build pipeline runs smoothly with our JavaScript tests. One error message we're facing is: An ...

"What is the method for verifying if a value is not a number in Angular 2

While attempting the following: dividerColor="{{isNaN(+price) ? 'warn' : 'primary'}}" An error is being thrown: browser_adapter.ts:82 ORIGINAL EXCEPTION: TypeError: self.context.isNaN is not a function Is there a way to determine ...

Understanding and processing HTML strings in typescript

I am currently utilizing TypeScript. Within my code, there is an object named "Reason" where all variables are defined as strings: value, display, dataType, and label. Reason = { value: '<ul><li>list item 1</li><li&g ...