I am facing below issue when using the Angular 5 Services, Can you please help me what is wrong with the below code
I have gone through below questions but didn't help me
I just want to initialize the value in the class and use it in the method
Though I have defined the url value in the class I'm getting error as Cannot read property 'url' of undefined
for the line console.log(this.url);
@Injectable()
export class OwnService {
public accessTokenvalue = '';
public url = 'https://www.someurl.com/id=';
constructor(private hp: HelpService) {
}
public gethpPhotos() {
this.hp.login()
.then(function (response: LoginResponse) {
console.log(response);
console.log(this.url);
})
.catch((error: any) => console.error(error));
}
}
You just need to replace function (response: Response)
with arrow function. Each function
creates it's scope to which refers this
inside it. Arrow function will preserve this
.
.then((response: LoginResponse) => {
console.log(response);
console.log(this.url);
})