Reputation: 8765
I have the following Service in Angular 6:
@Injectable()
export class LoginService {
constructor(private http: HttpClient) { }
login(): Observable<boolean> {
var url = `${environment.baseAPIUrl}${environment.loginUrl}`;
return this.http.get<boolean>(url);
}
}
I call it from my component:
@Component({
selector: 'bpms-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
constructor(private loginService: LoginService) { }
ngOnInit() {
}
login() {
var self = this;
self.loginService.login();
}
}
Why it doesn't send my request?
Upvotes: 1
Views: 2449
Reputation: 530
If your return type is Observable<> you need to subscribe to it, to get response once its ready.So your code will some thing like this
@Component({
selector: 'bpms-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
constructor(private loginService: LoginService) { }
ngOnInit() {
}
login() {
var self = this;
self.loginService.login().subscribe();
}
}
Upvotes: 1
Reputation: 23
You should call your login()
function, for example in ngOnInit()
or on form submit event, be sure that you call it.
and update your login()
function, look like this code:
login() {
this.loginService.login()
.subscribe((res) => console.log('login() response!'));
}
Upvotes: 1
Reputation: 136194
Observable
doesn't get called until you subscribe to it. Http
API method return Observable
, just by calling observable won't make and API call.
self.loginService.login().subscribe(
(data) => console.log('Logged in successfully')
);
Upvotes: 10
Reputation: 1960
Http Calls are not made until it is being subscribed to returned observable.
@Component({
selector: 'bpms-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
constructor(private loginService: LoginService) { }
ngOnInit() {
}
login() {
var self = this;
self.loginService.login().subscribe();
}
}
Upvotes: 3