gpt4 book ai didi

html - 带输入文件的 Angular 4/5 Material 凸起按钮

转载 作者:太空狗 更新时间:2023-10-29 18:04:15 25 4
gpt4 key购买 nike

我正在开发一个 Angular 应用程序,目前要上传我正在使用的文件:

<label class="btn btn-default">
<input type="file" (change)="selectFile($event)">
</label>
<button class="btn btn-success" [disabled]="!selectedFiles"
(click)="upload()">Upload</button>

使用我的 .ts 文件中的方法,它运行良好。

我现在想将其升级为 Material Angular 组件凸起按钮:

<button mat-raised-button>
<input type="file" (change)="selectFile($event)">
</button>

<button mat-button disabled [disabled]="!selectedFiles" (click)="upload()">Upload</button>

禁用按钮运行良好,但输入文件部分不起作用,打印效果很差,也无法打开文件夹搜索窗口。有什么想法吗?

最佳答案

不建议在按钮中使用输入字段,最好隐藏文件输入,然后隐藏一个按钮来触发它。下面的示例将显示它的一个最小示例

@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}} is for Uploading</h2>
</div>

<button mat-raised-button (click)="openInput()">
Select File to Upload
</button>

<input id="fileInput" hidden type="file" (change)="fileChange($event.target.files)" >

<button mat-button [disabled]="!ourFile" (click)="upload()">Upload</button>

`
})
export class App {
name:string;
ourFile: File; // hold our file

constructor() {
this.name = `Angular! v${VERSION.full}`
}

/**
* this is used to trigger the input
*/
openInput(){
// your can use ElementRef for this later
document.getElementById("fileInput").click();
}

fileChange(files: File[]) {
if (files.length > 0) {
this.ourFile = files[0];
}
}


/**
* this is used to perform the actual upload
*/
upload() {
console.log('sending this to server', this.ourFile);
}


}

检查这个plnk

通过上面的示例,您应该能够在不扭曲 HTML 语义的情况下设置按钮的样式

关于html - 带输入文件的 Angular 4/5 Material 凸起按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48578954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com