我正在尝试使用滑块控件将图像转换为灰度和深褐色
这是我的html代码
<div class="card-body">
<input id="sepia" type="range" oninput="set(this, 'sepia');" value="0" step="0.1" min="0" max="1"> Sepia <span id="Amount_sepia">(0)</span><br/>
<input id="grayscale" type="range" oninput="set(this, 'grayscale');" value="0" step="0.1" min="0" max="1"> Grayscale <span id="Amount_grayscale">(0)</span><br/>
</div>
<img class="img-fluid" id="img_prev" src="{{actualImage}}" *ngIf="!this.showCropper" />
<image-cropper id="img_prev" class="imageclass" *ngIf="this.showCropper"
[autoCrop]="false"
[imageChangedEvent]="imageChangedEvent"
[maintainAspectRatio]="true"
[aspectRatio]="4 / 3"
[resizeToWidth]="256"
[cropperMinWidth]="128"
[onlyScaleDown]="true"
format="png"
(imageCropped)="imageCropped($event)"
(imageLoaded)="imageLoaded()"
(cropperReady)="cropperReady()"
(loadImageFailed)="loadImageFailed()" style="max-height:500px">
</image-cropper>下面是我的答案
public set(e,f){
document.getElementById('img_prev').style["filter"] = f+"("+e.value+")";
document.getElementById('Amount_'+f).innerHTML="("+e.value+")";
}我收到错误消息
(index):13 Uncaught ReferenceError: set is not defined
at HTMLInputElement.oninput ((index):13)发布于 2021-02-03 04:20:16
为什么不使用“角度方式”呢?
您声明了两个变量
sepia=0;
grayScale=0;只需使用[(ngModel)]和[style.filter]
<input id="sepia" type="range" [(ngModel)]="sepia"
step="0.1" min="0" max="1"> Sepia
<span id="Amount_sepia">({{sepia}})</span>
<br/>
<input id="grayscale" type="range" [(ngModel)]="grayScale"
step="0.1" min="0" max="1"> Grayscale
<span id="Amount_grayscale">({{grayScale}})</span>
<br/>
<img [style.filter]="'grayscale('+grayScale+') sepia('+sepia+')'"
src="https://picsum.photos/300/300?random=1">发布于 2021-02-03 04:20:33
在上面的示例中,使用了oninput属性,它是一个global event attribute。这通常与普通的js一起使用,但在Angular中,还有一种在TypeScript代码中调用函数的方法:事件绑定。
在Angular中,不使用全局事件属性,而是使用Angular的事件绑定功能绑定到DOM Event。
它遵循应使用(input)="set(..params..)"将oninput="set(this, 'sepia');"更改为一种格式。
然而,一些适应似乎是必要的。例如,输入绑定中的this as参数将不起作用,$event很可能会包含必要的信息。另一方面,不推荐使用document.getElementById访问原生DOM元素,我建议使用ViewChild代替它。
https://stackoverflow.com/questions/66016717
复制相似问题