[英]Two way binding ngStyle
Im trying to make ngStyle
change its left
field value whenever i change something to my range value
. 我试图让
ngStyle
每当我将range value
更改为某值时ngStyle
更改其left
字段range value
。
i was inspired by how its done on: https://codepen.io/mayuMPH/pen/ZjxGEY but im not finding a way to do this in angular6-7. 我的灵感来自于它的完成方式: https ://codepen.io/mayuMPH/pen/ZjxGEY,但是我找不到在angular6-7中做到这一点的方法。
only when the document is loaded the left ngStyle
value will change. 只有在加载文档时,左
ngStyle
值才会更改。 but i dont get it to work when i drag the range. 但是当我拖动范围时,我没有使它起作用。 how can i achieve the
rs-bullet
to follow the value? 我如何实现
rs-bullet
跟随价值?
html code: html代码:
<div class="container">
<div class="range-slider">
<span id="rs-bullet" [ngStyle]="{'left':RangePos}" (change)="changeZoom()" class="rs-label" >{{ rangeValue }}</span>
<input id="rs-range-line" [(ngModel)]="rangeValue" class="rs-range" type="range" value="0" min="0" max="100">
</div>
<div class="box-minmax">
<span>0</span><span>200</span>
</div>
</div>
component: 零件:
rangeValue = 0;
RangePos = ((40 / 1000) * 578) + 'px';
ngOnInit() {
}
changeZoom() {
this.RangePos = ((this.rangeValue / 1000) * 578) + 'px';
}
You cannot use change
on span
because there is no such event on it. 您不能使用
span
change
,因为上面没有此类事件。 Actually, you don't even need it, because ngModel
should handle all the changes. 实际上,您甚至不需要它,因为
ngModel
应该处理所有更改。
So, to make your example working you should just change 因此,要使您的示例正常工作,您应该进行更改
changeZoom() {
return ((this.rangeValue / 1000) * 578) + 'px';
}
and 和
<span id="rs-bullet" [ngStyle]="{'left': changeZoom()}"
class="rs-label" >{{ rangeValue }}
</span>
Also divider 1000 in your example is incorrect because you input range max value is 100. 另外,示例中的分频器1000不正确,因为您输入的范围最大值是100。
Here you can see your example, working in Angular: https://stackblitz.com/edit/angular-66fr5v . 在这里,您可以看到在Angular中工作的示例: https : //stackblitz.com/edit/angular-66fr5v 。
Be sure, that you've added FormsModule
to the application module. 确保已将
FormsModule
添加到应用程序模块。 Otherwise ngModel
will not work. 否则
ngModel
将不起作用。
Can you try to set a function to bind Style like this? 您可以尝试设置一个功能来绑定样式吗?
<span id="rs-bullet" [ngStyle]="setRangePosStyles(RangePos)" (change)="changeZoom()" class="rs-label" >{{ rangeValue }}</span>
component: 零件:
setRangePosStyles(RangePos){
return {'left':RangePos};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.