Tuesday 27 December 2016

angular services uses within a function in a component



export class ChartTypeComponent implements OnInit {

constructor(private router: Router, private tService: TypeService) { }
ngOnInit() { }

vc_generate() {
this.tService.template_type$.subscribe((selectedTemplate) => {
console.log(selectedTemplate);
});
}

}



i need to call the service method from functions in the component...
When i call the service inside from the ngOnInit() it works fine,,but not works when used inside the function
is any wrong?


Answer



i solve the problem by changing Subject to BehaviorSubject in Service..so a subject does not hold values, it just passes them through. if you change from Subject to BehaviorSubject, then it would hold the value you sent it



now the value is printed in the console.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...