javascript - Angular 2 Links To External URLs Treated As Relative Paths For Routing - Stack Overflow

I've created a ponent which receives properties set on it's selector:<app-navigation-cardl

I've created a ponent which receives properties set on it's selector:

<app-navigation-card
        label="My Label"
        description="Description of Item"
        type="download"
        [links]="[{path:'.zip', label:'Download'}]"
></app-navigation-card>

The inputs are set up in the NavigationCard class:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-navigation-card',
  templateUrl: './navigation-cardponent.html',
  styleUrls: ['./navigation-cardponent.scss']
})
export class NavigationCardComponent implements OnInit {

  @Input() label: String;
  @Input() description: String;
  @Input() links: Object;
  @Input() type: String;

  constructor() { }

  ngOnInit() {
  }

}

In the template:

<div class="label">{{label}}</div>
<div class="description">{{description}}</div>
<ul *ngIf="links != undefined" class="links">
  <li *ngFor="let link of links" [routerLink]="link.path">
    <span *ngIf="type == 'download'"><a href="{{link.path}}">{{link.label}}</a></span>
    <span *ngIf="type == 'navigation'" [routerLink]="link.path"><{{link.label}}</span>
    <div></div>
  </li>
</ul>

If type == navigation, the router redirects to the correct ponent, but when it's a download, I'm getting this error:

EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'style-guide/https%3A/somedomain/somefile.zip'

This URL works fine when explicitly entered in the href of a link but not via property binding. Any idea how to remedy this?

I've created a ponent which receives properties set on it's selector:

<app-navigation-card
        label="My Label"
        description="Description of Item"
        type="download"
        [links]="[{path:'https://somedomain./somefile.zip', label:'Download'}]"
></app-navigation-card>

The inputs are set up in the NavigationCard class:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-navigation-card',
  templateUrl: './navigation-card.ponent.html',
  styleUrls: ['./navigation-card.ponent.scss']
})
export class NavigationCardComponent implements OnInit {

  @Input() label: String;
  @Input() description: String;
  @Input() links: Object;
  @Input() type: String;

  constructor() { }

  ngOnInit() {
  }

}

In the template:

<div class="label">{{label}}</div>
<div class="description">{{description}}</div>
<ul *ngIf="links != undefined" class="links">
  <li *ngFor="let link of links" [routerLink]="link.path">
    <span *ngIf="type == 'download'"><a href="{{link.path}}">{{link.label}}</a></span>
    <span *ngIf="type == 'navigation'" [routerLink]="link.path"><{{link.label}}</span>
    <div></div>
  </li>
</ul>

If type == navigation, the router redirects to the correct ponent, but when it's a download, I'm getting this error:

EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'style-guide/https%3A/somedomain./somefile.zip'

This URL works fine when explicitly entered in the href of a link but not via property binding. Any idea how to remedy this?

Share Improve this question edited Dec 20, 2016 at 15:40 Mistalis 18.3k14 gold badges77 silver badges97 bronze badges asked Dec 20, 2016 at 15:38 EricEric 2,0918 gold badges28 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You should consider creating an anchor tag as its seems to be an external URL which going to download a file. Rather remove *ngIf and just have 1st elements of *ngFor without span wrapper.

Additionally remove routerLink added to li element, which is getting fired the router.navigate function because of event bubbling.

<li *ngFor="let link of links">
    <a [attr.href]="link.path">{{link.label}}</a>
</li>

Or else have a click event & call function from controller, and open path in new tab using window.open(path). It will automatically download the file.

You can add https:// before the variable.

<a [attr.href]="'https://' + link.path">{{link.label}}</a>

Angular will recognize this as an absolute path, and redirect you to the URL.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303321a4621569.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信