I recently started learning angular and wanted to create a slider.
I created an array of objects which will hold data for the sliders images and tried to display them in my components html file like this:
Ts file:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-slider',
templateUrl: './slider.component.html',
styleUrls: ['./slider.component.scss']
})
export class SliderComponent implements OnInit {
constructor() { }
ngOnInit() {
type sliderImgMeta = Array<{id:number,src:string,alt:string}>;
const arr: sliderImgMeta = [
{id: 1, src: './img/slider1', alt: 'alt1'},
{id: 2, src: './img/slider2', alt: 'alt2'},
{id: 3, src: './img/slider3', alt: 'alt3'}
]
}
}
And the html file:
<section id="slider">
<div id="slider-content">
<img src="{{arr[0].src}}" alt="{{arr[0].alt}}">
</div>
<div id="slider-navigation">
<span id="moveLeft"><</span>
<span id="moveRight">></span>
</div>
</section>
In return im getting ERROR TypeError: Cannot read property '0' of undefined and unfortunetly have no idea how to resolve it.