Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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
  }
}
Comment

angular call component method from service

// In the service
export class simpleService{

  private subject = new Subject<any>();
  
  constructor() { }
  // Service message commands
  
  // Read Service Data
  onReadData(data: any){
    this.subject.next(data);
  }
  
  // Send Service Data 
  onSendData() {
    return this.subject.asObservable();
  }
  
}


// In the component you want to send the data to

dataSubscription: Subscription;

constructor(private simpleService: SimpleService){
	this.dataSubscription = this.simpleService.onSendData().subscribe( async (data) => {
      // do whaterver you want with the incoming data
      console.log('incoming data: ', data);
    });
}
 
Comment

PREVIOUS NEXT
Code Example
Javascript :: professional react projects 
Javascript :: jspdf create table 
Javascript :: api testing app with websocket 
Javascript :: setAttribute is not a function jquery 
Javascript :: cypress graphql request 
Javascript :: usememo hook react 
Javascript :: cypress json schema vs code 
Javascript :: javascript sort associative array 
Javascript :: moment diff 
Javascript :: nuxt 3 plugin 
Javascript :: object to map javascript 
Javascript :: odd or even js 
Javascript :: vue localstore 
Javascript :: check if the element exists in javascript 
Javascript :: refresh div after ajax success 
Javascript :: replace is not working in javascript 
Javascript :: javscript match word in string 
Javascript :: shouldcomponentupdate 
Javascript :: how to generate random color hexcodes for javascript 
Javascript :: change image on click javascript 
Javascript :: current date in mongodb 
Javascript :: run javascript in html 
Javascript :: javascript discord bot 
Javascript :: var x = 
Javascript :: nested function javascript 
Javascript :: remove beginning of base64 javascript 
Javascript :: Connect to socket.io node.js command line 
Javascript :: how to set window location search without reload 
Javascript :: capitalize first letter of each word 
Javascript :: handlebarsjs each first element 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =