I am trying to use method from the model class but its throwing error like
ERROR TypeError: act.sayHi is not a function
Here is my code
Model Class myModelClass.ts
export interface IMymodel {
name: string;
address: string;
age: number;
}
export class Mymodel implements IMymodel {
name: string;
address: string;
age: number
constructor() {}
sayHi(name): string {
console.log('Hiii' + name);
return 'Hiii'+name;
}
}
Component MyComponent.ts
import { IMymodel, Mymodel } from '../model/myModelClass.ts';
@Component({
selector: 'my-component',
templateUrl: 'myComponent.html'
})
export class MyComponent {
...
prepareData(data: Array<IMymodel>): Array<IMymodel> {
data.map((act: Mymodel) => {
act.sayHi(act.name);
});
return data;
}
}
You need to put the sayHi
method signature into the interface too as you access your objects via interface type and that type lacks a method with name sayHi
.
export interface IMymodel {
name: string;
address: string;
age: number;
sayHi(name): string
}
and method call
prepareData(data: Array<IMymodel>): Array<IMymodel> {
data.map((act: IMymodel) => {
act.sayHi(act.name);
});
return data;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.