1

I would like to get in angular 6 component. I managed it by: PlatformLocation#getBaseHrefFromDOM.

But this api should not be used by developers. Is there any other way to get /CTX-ROOT/assets/tiny-editor/langs/cs.js in runtime?

  constructor(
    private zone: NgZone,
    private platformLocation: PlatformLocation) {
  }

  public ngAfterViewInit() {
    var baseHref = this.platformLocation.getBaseHrefFromDOM();
    Observable.fromPromise(tinymce.init({
      selector: '#' + this.elementId,
      entity_encoding: "raw",
      menubar: false,
      branding: false,
      elementpath: true,
      language_url: baseHref + '/assets/tiny-editor/langs/cs.js',
Petr Prikryl
  • 13
  • 1
  • 5

2 Answers2

3

You can use the prepareExternalUrl method from the Location service (doc)

import {Location} from "@angular/common";

//...
constructor(
    private zone: NgZone,
    private location: Location) {
}


public ngAfterViewInit() {
    Observable.fromPromise(tinymce.init({
      selector: '#' + this.elementId,
      entity_encoding: "raw",
      menubar: false,
      branding: false,
      elementpath: true,
      language_url: this.location.prepareExternalUrl('assets/tiny-editor/langs/cs.js')

Edit: I assume this only works when using PathLocationStrategy

This method will also add a hash if HashLocationStrategy is used, or the APP_BASE_HREF if the PathLocationStrategy is in use.

Community
  • 1
  • 1
David
  • 33,444
  • 11
  • 80
  • 118
-3

How about you use native JS for it? window.location will give the Location object. window.location.origin will give you the base href.

Ansuman
  • 1,404
  • 4
  • 14
  • 32
  • this returns window.location.origin "http://localhost:3000" instead of "http://localhost:3000/xxx/" – Petr Prikryl Sep 12 '18 at 13:06
  • window.location gives an object where there are a number of information. You could use "origin" and "pathname" to get any part of url you want. – Ansuman Sep 12 '18 at 13:34