我正在使用17号角和17号角 转录者:

link

我的目的是有一个或多个预选文件显示在选定的文件列表.正如你所看到的,我在ngAfterViewInit中调用this.fileUpload._files.push(file),预期的结果是文件列表中有文件.相反,它只在单击" Select "时显示.我还添加了一个变化检测器,但它仍然不工作.

我怎样才能做到这一点呢?

推荐答案

我调用了FileUpload组件内部ChangeDetectorRef中的detectChanges,然后文件开始出现!

 ngAfterViewInit(): void {
    if (this.fileUpload) {
      this.fileUpload._files.push(this.file);
      this.fileUpload.cd.detectChanges(); //   <-changed here!
      this.cdr.detectChanges();
    }
  }

完整代码

import {
  AfterViewInit,
  ChangeDetectorRef,
  Component,
  ViewChild,
  inject,
} from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';
import { FileUpload, FileUploadModule } from 'primeng/fileupload';

@Component({
  selector: 'app-root',
  standalone: true,
  templateUrl: './app.html',
  imports: [FileUploadModule],
})
export class App implements AfterViewInit {
  cdr = inject(ChangeDetectorRef);
  file = new File([''], 'filename');

  @ViewChild('fileUpload') fileUpload!: FileUpload;

  ngAfterViewInit(): void {
    if (this.fileUpload) {
      this.fileUpload._files.push(this.file);
      this.fileUpload.cd.detectChanges();
      this.cdr.detectChanges();
    }
  }
}

bootstrapApplication(App);

Stackblitz Demo

Angular相关问答推荐

Angular 信号效果,try 重置表单并获取在计算或效果中不允许写入信号"

如何go 除融合图中的拖尾水印?

NG构建后多余的国旗

在Angular 多选下拉菜单中实现CDK拖放排序的问题

如何在ANGLING v17中使用鞋带样式组件?

在剑道UI网格中包装Excel导出的列

Angular 应用程序未在Azure应用程序服务中运行

nx serve正在忽略@angular/localize/init的导入"

Angular 17水化-POST请求同时触发客户端/服务器端,而GET请求仅触发服务器端?

如何以Angular 解析表单控件名称的无值访问器?

Angular CLI 与 Angular 版本不兼容

由于ngcc操作失败,Angular扩展可能无法正常工作

Angular6:RxJS switchMap 运算符未按预期工作

显示 1 个数据而不是所有 ngFor - Angular 11

如何检测滚动到html元素的底部

如何从Angular 2 中的按钮单击触发输入文件的单击事件?

Angular - 是否有 HostListener-Events 列表?

将外部 CSS 加载到组件中

formGroup 需要一个 FormGroup 实例

如何在Angular中使用带有 td 属性 colspan 的属性绑定?