calling angular component method in service

Solutions on MaxInterview for calling angular component method in service by the best coders in the world

showing results for - "calling angular component method in service"
Michele
03 Jan 2021
1// -------------------------------------------------------------------------------------
2// codes for component
3import { JustAService} from '../justAService.service';
4@Component({
5  selector: 'app-cute-little',
6  templateUrl: './cute-little.component.html',
7  styleUrls: ['./cute-little.component.css']
8})
9export class CuteLittleComponent implements OnInit {
10  s: JustAService;
11  a: number = 10;
12  constructor(theService: JustAService) {
13    this.s = theService;
14  }
15
16  ngOnInit() {
17    this.s.onSomethingHappended(this.doThis.bind(this));
18  }
19
20  doThis() {
21    this.a++;
22    console.log('yuppiiiii, ', this.a);
23  }
24}
25// -------------------------------------------------------------------------------------
26// codes for service
27@Injectable({
28  providedIn: 'root'
29})
30export class JustAService { 
31  private myFunc: () => void;
32  onSomethingHappended(fn: () => void) {
33    this.myFunc = fn;
34    // from now on, call myFunc wherever you want inside this service
35  }
36}
37
queries leading to this page
how to call the component inside a service the code in angularhow to call a method from a component in an angular servicecall service in component angular 8 examplecreate a method inside service angularjshow to use function from a service in a component in angularhow to call component function from service in angularcall service in component angular 8how to call service in component angularhow you are calling service into component angularcan services call a component in angualarangular call service from componenthow to call service method in component in angularangular call function in component from serviceservice call in angular examplehow to call component from service in angular 6call component from service angularhow to call an angular service classis it possible to call a method inside component by service in angularimport method from component to service angularangular component call service on initangular component call method from componentangular call method on componenthow to call a service method inside anothe rin angularjsangular call service method from htmlangular service call function in componentshould we call service function from angular component viewcall component method from service angularin a class can we call a service angularcalling angular component method in servicehow to call component method from service in angular 8how to call a method inside component in service in angularhow to call a method in service file from a component in angularhow to call the component inside a service in angularangular calling a serviceangular call component function from serviceangular call service method from componenthow to call method in component angular 11can we call method from another service in angularcall service in angular componentangular service callhow to call service method from html in angularservice call in angularcall service method from dom angularhow to call a service method inside anothe rin angularcall service in angular component examplecalling angular component method in service