我正在使用Angular Material v6。
文档说它displaywith是“将在其显示在拇指标签中之前用于格式化该值的函数”。它应该这样使用:
<mat-slider thumbLabel [displayWith]="formatRateLabel" [value] = 'movie.mark'>
</mat-slider>
Run Code Online (Sandbox Code Playgroud)
在我的组件类中,我有一个formatRateLabel函数:
formatRateLabel(value: number) {
if (!value)
return 0;
return "HI!"; //just for demonstration
}
Run Code Online (Sandbox Code Playgroud)
我需要从该函数内部获取组件的值。我通常通过this在某些函数中使用关键字来执行此操作。但是,当我从该fornatRateLabel组件中执行该功能时,组件的属性是不确定的。据我了解,这是一种范围问题。this此功能中的Word 只能访问mat-slider的所有属性和功能。但是我需要从该函数访问组件的属性。让我展示一下:这是我的组件:
@Component({
templateUrl: './movieLibrary.component.html',
styleUrls: ['./movieLibrary.component.css']
})
export class MovieLibraryComponent{
someProperty : boolean = true;
formatRateLabel(value: number){
var myLocalVariable = this.someProperty;// debug showed it's as undefined. Why?
}
}
Run Code Online (Sandbox Code Playgroud)
我试图传递一些参数来formatRateLabel
像这样
[displayWith]="formatRateLabel(movie.mark)"
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,拇指标签将变为空,因此也无法使用。
有什么建议么?
在过去的几周里,我开始在 C# 中遇到这种新语法:
if (someObj is { })
{
do some stuff
}
Run Code Online (Sandbox Code Playgroud)
所以它返回布尔值。看起来有点像 JavaScript。但这项检查到底有什么作用呢?和这个一样吗?
if (someObj == null)
{
do some stuff
}
Run Code Online (Sandbox Code Playgroud)
我知道新版本的 C# 包含很多语法糖。这是其中的一部分吗?它有名字或什么吗?例如,我知道这?:被称为三元运算符,并且?.被称为 Elvis 运算符。但什么是is { }?甚至是运营商吗?
is { }是的,我在提问之前尝试过在线搜索,但由于请求中的大括号,谷歌似乎拒绝找到任何有关语法的有用信息。