I want to inject my FooService and use it only when I click on the button.
But I got this error:
Error: NG0203: inject() must be called from an injection context (a constructor, a factory function or a field initializer)
Is there a way to make it work by inject will be in a separate function?
import { Component, inject, VERSION } from '@angular/core';
import { FooService } from './foo.service';
const load = () => {
const foo = inject(FooService);
foo.doSome();
};
@Component({
selector: 'my-app',
template: `in app! <button (click)="onClick()">click </button>`,
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
onClick() {
load();
}
}