【问题标题】:Angular 2 click on <a> does not go to linkAngular 2 点击 <a> 不会转到链接
【发布时间】:2016-10-20 16:34:40
【问题描述】:

在一个组件中我有这样的东西:

<a class="btn" [attr.href]="movie.imdbUrl" target="_blank" >IMDb</a>

但是当我点击它时没有任何反应。

movie.imdbUrl 指向“http://www.imdb.com/title/tt2305051/”,我认为它相当于:

<a class="btn" href="http://www.imdb.com/title/tt2305051/" target="_blank" >IMDb</a>

我错过了什么?

【问题讨论】:

    标签: angular


    【解决方案1】:

    也许不是最优雅的方式,但绝对应该可行:

    How to redirect to an external URL in Angular2

    window.location.href = '...';
    

    【讨论】:

    • 我的问题与 Angular 2 有关。Angular 2 中不再有 ng-href。
    • 对不起,我认为它的角度为 1,我已经删除了旧帖子
    【解决方案2】:

    Thix 示例对我有用:

     import { Component } from '@angular/core';
    
     @Component({
         selector: 'my-app',
         template: `
            <a class="btn" [attr.href]="movie.imdbUrl" target="_blank" >IMDb</a>
            `
    })
    export class AppComponent {
        movie = {
           imdbUrl: "http://www.imdb.com/title/tt2305051/"
        }
    }
    

    你确定你在你的组件中正确定义了电影对象吗?

    【讨论】:

    • 我做到了,但我在 id 属性中也有一个拼写错误,它也读作“电影”。这让 Angular 感到困惑。不能说是我的错。感谢所有回复的人。
    猜你喜欢
    • 2012-10-06
    • 2017-10-29
    • 2014-01-05
    • 1970-01-01
    • 2011-06-24
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多