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 badges2 Answers
Reset to default 3You 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条)