ionic2中的隐藏元素:不能绑定到“*ngIf”,因为它不是已知的本地属性。

时间:2022-08-25 09:32:24

I have a problem with DOM elements in my ionic2 app. When I trying do something like this:

我在ionic2应用程序中有一个DOM元素的问题,当我尝试这样做的时候:

<ion-tabs>
  <ion-tab [root]="tab1Root" tabTitle="el1" tabIcon="list-box"></ion-tab>
  <ion-tab [root]="tab2Root" tabTitle="el2" tabIcon="git-pull-request"></ion-tab>
  <div *ngIf="'admin'=='admin'">
    <ion-tab [root]="tab4Root" tabTitle="Admin" tabIcon="cog"></ion-tab>
  </div>
  <ion-tab [root]="tab3Root" tabTitle="Profile" tabIcon="person"></ion-tab>
</ion-tabs>

everything is OK. But when I set angular variable in constructor:

一切都是好的。但当我在构造函数中设置角变量时

export class TabsPage {
  constructor() {
    this.userRole = "admin";

and

<ion-tabs>
      <ion-tab [root]="tab1Root" tabTitle="el1" tabIcon="list-box"></ion-tab>
      <ion-tab [root]="tab2Root" tabTitle="el2" tabIcon="git-pull-request"></ion-tab>
      <div *ngIf="{{userRole}}=='admin'">
        <ion-tab [root]="tab4Root" tabTitle="Admin" tabIcon="cog"></ion-tab>
      </div>
      <ion-tab [root]="tab3Root" tabTitle="Profile" tabIcon="person"></ion-tab>
    </ion-tabs>

the application returns me an error:

应用程序返回错误:

Can't bind to '*ngIf' since it isn't a known native property

因为它不是已知的本地属性,所以不能绑定到'*ngIf'。

How I can hide this element when userRole = 'admin'?

当userRole = 'admin'时,如何隐藏这个元素?

1 个解决方案

#1


3  

The condition expression at *ngIf="expression" is already going to be evaluated, no need to use {{}}.

在*ngIf="表达式"的条件表达式已经被评估,不需要使用{{}}。

So

所以

<div *ngIf="{{userRole}}=='admin'">

should really be

真正应该

<div *ngIf="userRole=='admin'">

#1


3  

The condition expression at *ngIf="expression" is already going to be evaluated, no need to use {{}}.

在*ngIf="表达式"的条件表达式已经被评估,不需要使用{{}}。

So

所以

<div *ngIf="{{userRole}}=='admin'">

should really be

真正应该

<div *ngIf="userRole=='admin'">