如何从angular8打开不安全的URL?

时间:2019-07-31 21:38:11

标签: angularjs

我正尝试从angular8 UI如下打开超链接change://problem/12345678,但是它总是被重定向到unsafe:change://problem/12345678,但没有打开链接,我看着AngularJS changes URLs to "unsafe:" in extension page并尝试使用{ {1}}并遇到以下错误,在angular8中有更简单的方法吗?

ng-href

错误:-

            <table mat-table [dataSource]="newPost.posts" multiTemplateDataRows
                class="table table-bordered table-info" style="text-align:center" *ngIf="enteredValue != ''">
                <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay; let idx=index">
                    <th style="color: black;" mat-header-cell *matHeaderCellDef> {{column}} </th>
                    <span *ngIf="idx == 0">
                        <td style="color: black;" mat-cell *matCellDef="let element"> 
                            <a href="change://problem/{{element[column]}}">{{element[column]}} </a> </td>
                    </span>
                    <span *ngIf="idx == 5">
                        <table mat-cell *matCellDef="let element"> 
                            <tr *ngFor="let gerrit of element[column]">
                                <a href={{gerrit}} target="_blank">{{gerrit}} </a>
                            </tr> 
                        </table>
                    </span>
                    <span *ngIf="idx != 0 && idx != 5">
                        <td style="color: black;" mat-cell *matCellDef="let element"> {{element[column]}} </td>
                    </span>
                </ng-container>

1 个答案:

答案 0 :(得分:3)

您必须使用DomSanitizer#bypassSecurityTrustUrl

class FooComponent {
  public constructor(private sanitizer: DomSanitizer) {}

  public getUrl(column: string) {
    return this.sanitizer.bypassSecurityTrustUrl(`change://problem/${column}`)
  }
}

并在模板中:

<a [attr.href]="getUrl(element[column])">{{ element[column] }}</a>