i cant define a value for students because of typescript says it cant be used as index type note : i declared id as number not Number and Name as string not String
import { Component } from '@angular/core';
interface student {
Id: number;
Name: string;
}
@Component({
selector: 'sameh-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
students: Array<student> = [];
color: string = 'color: red';
color2: string = 'color : blue';
userName: string = '';
title = 'ng-arab';
constructor() {
this.students[{ Id: 0, Name: 'sameh' }];
}
}
CodePudding user response:
prueba esto:
import { Component } from '@angular/core';
interface student {
Id: number;
Name: string;
}
@Component({
selector: 'sameh-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
students: Array<student> = [];
color: string = 'color: red';
color2: string = 'color : blue';
userName: string = '';
title = 'ng-arab';
constructor(enter code here) {
this.students[{ "Id": 0, "Name": 'sameh' }];
}
}
CodePudding user response:
I guess you are trying to push an element inside the array on the constructor, change your code to.
import { Component } from '@angular/core';
interface student {
Id: number;
Name: string;
}
@Component({
selector: 'sameh-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
students: Array<student> = [];
color: string = 'color: red';
color2: string = 'color : blue';
userName: string = '';
title = 'ng-arab';
constructor() {
this.students.push({ Id: 0, Name: 'sameh' }); // <- changed here
}
}