0

I want to make a list and I'm trying to print the selected line from the list to the top block. But I am encountering a problem while creating an object. I tried the answers to the previously asked questions, but it did not work. The typescript version I'm using is 4.4.

What i want:

What i want show with pic

What happened:

What happened

This is problem: error TS2564: Property 'selectedMovie' has no initializer and is not definitely assigned in the constructor.

movie.ts

export class Movie{
     id:number;
     name:String;

     constructor(id:number,name:String){
    this.id = id;
    this.name = name;
   }
}

movie.datasource.ts

import { Movie } from "./movie";

export const Movies: Movie[] = [
    {id:1, name:"movie 1"},
    {id:2, name:"movie 2"},
    {id:3, name:"movie 3"}
]

movies.component.ts

import { Component } from "@angular/core";
import { Movie } from "../movie";
import {Movies} from "../movie.datasource"

@Component({

    selector: 'movies', // <movies> </movies>
    templateUrl: 'movies.component.html',
    styleUrls: ['./movies.component.css']

})

export class MoviesComponent{
    title = "Movie List";
    movies = Movies;

    
    selectedMovie: Movie; //PROBLEM HERE

    
    onSelect(movie:Movie): void{
        this.selectedMovie = movie;
    }
 }

movies.component.html

 <div class="card">
     <div class="card-body">
        <span class="badge badge-warning">
            id: {{selectedMovie.id}} name: {{selectedMovie.name}}
        </span>
    </div>
</div>


<div class="card">
    <div class="card-header">
        {{title}}
    </div>
    <ul class="list-group list-group-flush">
        <li *ngFor="let movie of movies" (click)="onSelect(movie)"
          class="list-group-item"
          [class.active] = "movie === selectedMovie">
          <span class="badge badge-primary">{{movie.id}}</span>{{movie.name}}
          <span class="close">x</span>
        </li>
    </ul>
 </div>
  • Does this answer your question? [Property '...' has no initializer and is not definitely assigned in the constructor](https://stackoverflow.com/questions/49699067/property-has-no-initializer-and-is-not-definitely-assigned-in-the-construc) – Yong Shun Nov 10 '21 at 09:03
  • no i tried it its not working. Already after reading this I continued the project by adding this "strictPropertyInitialization": false – Alper Yıldırım Nov 10 '21 at 10:13
  • 1
    Change to `selectedMovie?: Movie;` so that it is nullable. Will not recommend to change the tsconfig.json with `"strictPropertyInitialization": false` as it will impact to whole project. – Yong Shun Nov 10 '21 at 10:22
  • @YongShun there is an error like this (error TS2532: Object is possibly 'undefined'. 5 id: {{selectedMovie.id}} name: {{selectedMovie.name}}) – Alper Yıldırım Nov 10 '21 at 10:28
  • Use Optional chaining `?.`. `id: {{selectedMovie?.id}} name: {{selectedMovie?.name}}`. It will not access to inner properties when `selectedMovie` was `null` or `undefined`. – Yong Shun Nov 10 '21 at 10:35
  • @YongShun yes, something came to the screen in this way, but when I select or click, there is no interaction in the top row, it remains blank. – Alper Yıldırım Nov 10 '21 at 10:52
  • See this [demo](https://stackblitz.com/edit/angular-ivy-ducruk?file=src/app/movies/movies.component.html). No problem from my side. – Yong Shun Nov 10 '21 at 11:27
  • I really can't understand. How can that be? I wonder if there is something missing on my computer, after all, the codes are the same and it does not give the same result on my computer. But thank you very much for posting this, I'm very happy that the code works correctly. at least our code is correct :) – Alper Yıldırım Nov 10 '21 at 11:53
  • @YongShun could you please share your typescript version and angular version with me? – Alper Yıldırım Nov 11 '21 at 11:39
  • You can check on the demo link, left bottom Dependencies section. – Yong Shun Nov 11 '21 at 11:43

1 Answers1

0

Convert your movie class to interface.

export interface Movie {
     id:number,
     name:String
}
Drashti Dobariya
  • 2,455
  • 2
  • 10
  • 23