嗨,我正在尝试编写* ngIf语句,该语句将通用字符串作为输入,这将依次返回true / false 例如
下面是示例组件html和ts文件。
<div>
<p *ngIf='hideExpression'>
This will be visible only if employee member is employee.
</p>
</div>
users.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent implements OnInit {
hideExpression = '';
isEmployee = false;
constructor() { }
ngOnInit() {
//Note: I am going to fetch this expression from Database which is stored in string form
this.hideExpression = 'isEmployee === true';
}
}
我的成员变量hideExpression
包含一个有效的字符串表达式,该表达式将返回true或false。如何使用此hideExperssion
作为*ngIf
的参数
答案 0 :(得分:0)
更改为entry/x:=master.read()
this.hideExpression = this.isEmployee === true;
更新
如果要从数据库中获取hideExpression,则需要创建服务并像这样从DB中获取数据
ngOnInit() {
this.hideExpression = this.isEmployee === true;
}
答案 1 :(得分:0)
您不能将表达式放在字符串中使用方法
<div>
<p *ngIf='isEmployee'>
This will be visible only if employee member is employee.
</p>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.css']
})
export class UsersComponent implements OnInit {
isEmployee = false;
constructor() { }
hideExpression();
ngOnInit() {
}
private hideExpression(){
return !this.isEmployee;
}
}