Can we set the APP_BASE_HREF in angular 6 dynamically by getting the value from http call in a service?

Viewed 228

This is the code from app.module.ts file

{
  provide: APP_BASE_HREF,
  useFactory: referalCodeValidator,
  deps: [ValidatorService]
}

There is a referalCodeValidator function defined in the app.module.ts file itself

export function referalCodeValidator(common: ValidatorService) {
  let path = location.pathname;
  let refCode = path.split("/", 2);
  return common.validateReferralCode(refCode[1]);
}

The validator service is as follows:

import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { environment } from "../../environments/environment";
import { HttpClient } from "@angular/common/http";
import { tap, catchError } from "rxjs/operators";

@Injectable()
export class ValidatorService {
  appBaseHref: any;
  constructor(private http: HttpClient) {}

  validateReferralCode(refCode): Observable<any> {
    if (refCode != "" || refCode != null) {
      this.appBaseHref = null;
      return this.http
        .get(someurl, {
          params: {
            referenceCode: refCode
          }
        })

        .pipe(catchError((error, caught) => {
          console.log(error);
          throw error.error;
        }) as any);
      }
    }
}
0 Answers
Related