javascript - Nz-Slider 格式化程序输入,nz-zorro-9.3.1
问题描述
滑块有一些奇怪的问题。我想将滑块的值更改为滑块所在组件中的另一个值。使用输入格式化程序时,一切都很好,但是当我使用“this.slider”时,它返回我未定义。因为格式化函数在通过输入传递它时会丢失它的上下文。为了清楚起见,我举了一个例子
export class ConfigurationRulesListComponent implements OnInit {
@ViewChild('slider', { static: false }) sliderComponent: NzSliderComponent;
listOfValues: Array<string> = [];
currentValue = 0;
minValue = 0;
maxValue = 0;
stepMarks: Marks = {};
constructor() {}
ngOnInit() {
this.getValuesFromBackend();
this.init();
}
formatter(index: number): string {
const marksFound: Marks = this.sliderComponent.nzMarks; // here undefine beccose, is other context
const label = (marksFound[index] as MarkObj).label;
const result = new RegExp(/(?<=<strong>\s*).*?(?=\s*<\/strong>)/);
const matchResults = label.match(result);
return `${matchResults[0]}`;
}
private init() {
// para construir las marcas en caso de que tengan estilos determinados
for (let i = 0; i < this.listOfValues.length; i++) {
const newMark = new Marks();
newMark[i] = {
style: {
color: '#f50'
},
label: `<strong>${this.listOfValues[i]}</strong>`
};
this.stepMarks = {
...this.stepMarks,
...newMark
};
}
}
private getValuesFromBackend() {
// adicionarle observables
this.listOfValues = ['First', 'Second', 'Third', 'fourth', 'fifth', 'sixth'];
this.currentValue = 0;
this.minValue = 0;
this.maxValue = this.listOfValues.length - 1;
}
}
html>
<nz-slider
#slider
[nzMin]="minValue"
[nzMax]="maxValue"
[nzMarks]="stepMarks"
[(ngModel)]="currentValue"
[nzTipFormatter]="formatter"
></nz-slider>
解决方案
推荐阅读
- search - 问:Atom 编辑器上的大写/小写搜索问题
- python - 尝试使用 Sendinblue/Sendgrid SMTP 发送忘记密码的电子邮件时出现 SMTPRefused 错误
- git - Git:与 Funtoo 钥匙串集成
- java - Java 找不到文件作为本机代码颤动
- android - 尝试在 Android Studio 中运行时出现 AVD 错误
- python - 在使用集合 Dijkstra Shortest Path Algorithim 的字典中删除给定节点时出现 KeyError
- node.js - 从不同文件夹执行节点 cli 应用程序时,dontenv(npm 模块)不加载正确的 .env 文件
- logging - 如何使用 NLog 直接记录到 Datadog
- wordpress - 如何正确扩展 Gutenberg 块核心/引用块?
- python - 在 Django TEMPLATES 中设置 DIRS url 时遇到问题