Log*_*Wlv 3 html material angular-material angular
我正在开发一个角度应用程序,目前正在上传我正在使用的文件:
<label class="btn btn-default">
    <input type="file" (change)="selectFile($event)">
</label>
<button class="btn btn-success" [disabled]="!selectedFiles"
    (click)="upload()">Upload</button>
Run Code Online (Sandbox Code Playgroud)
使用我的.ts文件中的方法,它运行良好.
我想将此升级为物质角度组件凸起按钮,就像现在一样:
<button mat-raised-button>
    <input type="file" (change)="selectFile($event)">
</button>
<button mat-button disabled [disabled]="!selectedFiles" (click)="upload()">Upload</button>
Run Code Online (Sandbox Code Playgroud)
禁用按钮运行良好,但输入文件部分不起作用,打印错误,不打开文件夹搜索窗口.有任何想法吗?
The*_*bee 12
不建议使用按钮内的输入字段,更好地隐藏文件输入,然后按一个按钮来触发它.以下示例将显示它的最小示例
@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);
  }
}
Run Code Online (Sandbox Code Playgroud)
检查这个plnk
通过上面的示例,您应该能够设置按钮样式而不会扭曲HTML语义
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           6715 次  |  
        
|   最近记录:  |