calling angular component method in service

// -------------------------------------------------------------------------------------
// codes for component
import { JustAService} from '../justAService.service';
@Component({
  selector: 'app-cute-little',
  templateUrl: './cute-little.component.html',
  styleUrls: ['./cute-little.component.css']
})
export class CuteLittleComponent implements OnInit {
  s: JustAService;
  a: number = 10;
  constructor(theService: JustAService) {
    this.s = theService;
  }

  ngOnInit() {
    this.s.onSomethingHappended(this.doThis.bind(this));
  }

  doThis() {
    this.a++;
    console.log('yuppiiiii, ', this.a);
  }
}
// -------------------------------------------------------------------------------------
// codes for service
@Injectable({
  providedIn: 'root'
})
export class JustAService { 
  private myFunc: () => void;
  onSomethingHappended(fn: () => void) {
    this.myFunc = fn;
    // from now on, call myFunc wherever you want inside this service
  }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
should we call service function from angular component view how to call an angular service class calling angular component method in service how to call the component inside a service in angular how to call the component inside a service the code in angular call service in angular component example call service in component angular 8 call service in component angular 8 example how you are calling service into component angular angular component call method from component in a class can we call a service angular angular call method on component call component method from service angular how to call component function from service in angular angular calling a service import method from component to service angular how to call a service method inside anothe rin angularjs how to call a service method inside anothe rin angular how to call a method in service file from a component in angular how to call component method from service in angular 8 how to call service method in component in angular angular component call service on init how to call service method from html in angular how to call method in component angular 11 service call in angular example can we call method from another service in angular angular call service method from html angular call service method from component call service in angular component angular service call create a method inside service angularjs angular call service from component Call service method from dom angular angular service call function in component angular call function in component from service call component from service angular service call in angular how to call component from service in angular 6 how to call service in component angular
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source