Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove base-href from a external link in angular?

Tags:

angular

From my database i am getting the information about organizations. Here is the interface structure for organizations:

interface IOrganization {
  id: number;
  name: string;
  url: string;
  createdAt: Date;
}

now i want to display them like this:

<div *ngIf="isloading; else content">
  <mat-progress-bar mode="indeterminate" color="warn"></mat-progress-bar>
</div>

<ng-template #content>
  <mat-tab-group>
    <mat-tab label="Organizations List" >
      <mat-card 
        class="example-large-box mat-elevation-z4"
        *ngFor="let org of organizations"
      >
        <mat-nav-list>
          <a mat-list-item href="{{ org.url }}"> {{ org.name }} </a>
        </mat-nav-list>
      </mat-card>
    </mat-tab>
  </mat-tab-group>
</ng-template>

Unfortunently link is not working. Whenever i do mousehover on the link, i am getting the information including the base href.

Let say my organization link is www.google.com. and whenever someone click on then user should redirect to the organizations. But i am getting something like this whenever i click on the links.

http:// localhost:4200/organizations/www.google.com

How can i get rid of from the base href from my links so that user can easily go to google.com while he click on the links?

like image 504
Kazi Avatar asked Mar 27 '19 13:03

Kazi


People also ask

What is-- base href in Angular?

The base href is the URL prefix that should be preserved when generating and recognizing URLs.

Why we use base href in Angular?

The <base href="/"> tells the Angular router what is the static part of the URL. The router then only modifies the remaining part of the URL. Now that we've set the our base href tag, we can then move on to defining some routes without your route module file or your app.

What is base href?

The href attribute specifies the base URL for all relative URLs on a page.

How would you add a hyperlink in a view in Angular 4?

If you are using routing and want to add a href attribute to a specific route. Use the routerLink directive as it will add the href attribute to <a> tags.


2 Answers

Ah i got the answer:

<a mat-list-item [attr.href]="'//'+ org.url "> {{ org.name }} </a>

This line of code solves the problem. for more detail please click on this link

like image 71
Kazi Avatar answered Nov 15 '22 06:11

Kazi


The problem is that Angular is treating the URL www.google.com as a relative link and appending it to the current route as if it exists on the site.

You have to use an absolute link, like http://www.google.com to actually go to that site instead of appending it to the current route.

like image 37
Nerdy Avatar answered Nov 15 '22 05:11

Nerdy