*ngIf window.location.href.indexOf 不工作

eri*_*mmy 0 html javascript angular

仅当位置正确时我才需要显示一个元素,否则不应该显示。

这不起作用:

<div *ngIf="!accessTrue() && window.location.href.indexOf('something')" > -1)>
    CODE
</div>
Run Code Online (Sandbox Code Playgroud)

n00*_*dl3 5

您无法访问window模板内的对象。但是你可以在你的组件中定义一个 getter :

get hasSomething(){
    return window.location.href.indexOf('something') > -1
}
Run Code Online (Sandbox Code Playgroud)

然后 :

<div *ngIf="!accessTrue() && hasSomething">
    CODE
</div>
Run Code Online (Sandbox Code Playgroud)

请注意,ActivatedRoute如果您的参数可以通过它访问,则使用它可能会更干净。