模态和输入类型文件角

发布于 2025-02-06 13:43:32 字数 1183 浏览 0 评论 0原文

我为个人项目(社交网络)工作。用户可以创建,修改ET删除帖子,他们可以评论我

在Angular上工作的

每篇文章,我有一个显示帖子的后列表组件。 我创建一个打开模态以显示表单以

    displayForm() {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = false;
    dialogConfig.autoFocus = true;
    dialogConfig.width = "600px";
    dialogConfig.maxWidth = "80%";
    this.dialog.open(FormPostComponent, dialogConfig);
}

在FormPostComponent的html上创建帖子的finction,如果用户要上传图像,我有一个输入类型文件,

<div>
    <label for="file" class="label-file " id="label">Ou choisir une image</label>
    <input
        #fileinput
        type="file"
        class="input-file"
        id="file"
        formControlName="file"
        (change)="addFile($event)"
    />
</div>
   

问题是我无法访问该文件,文件,文件似乎是由模式背后的另一个输入来检索的。

我在我的ModalCompononent中找到了一个带有设置的解决方案:endapsulation:viewScapsulation.shadowdom,但我不认为这是解决方案,因为所有浏览器不支持Shadowdom

@Component({
selector: "app-form-post",
templateUrl: "./form-post.component.html",
styleUrls: ["./form-post.component.scss"],
encapsulation: ViewEncapsulation.ShadowDom,

})

输入更改的路径

谢谢您

i work for a personal project (a social network). Users can create, modify et delete Post and they can comment each post

I work on Angular

I have a post-list component which display posts.
I create a fonction to open a modal to display a Form to create a post

    displayForm() {
    const dialogConfig = new MatDialogConfig();
    dialogConfig.disableClose = false;
    dialogConfig.autoFocus = true;
    dialogConfig.width = "600px";
    dialogConfig.maxWidth = "80%";
    this.dialog.open(FormPostComponent, dialogConfig);
}

On the Html of FormPostComponent i have an input type File if the user want to upload an image

<div>
    <label for="file" class="label-file " id="label">Ou choisir une image</label>
    <input
        #fileinput
        type="file"
        class="input-file"
        id="file"
        formControlName="file"
        (change)="addFile($event)"
    />
</div>
   

The problem is i can't have access to the file, the file seems to be retrieved by another input behind the modal.

I find a solution with set in my modalCompononent : encapsulation : ViewEncapsulation.ShadowDom but i don't think it's the solution because ShadowDom is not supported with all the browsers

@Component({
selector: "app-form-post",
templateUrl: "./form-post.component.html",
styleUrls: ["./form-post.component.scss"],
encapsulation: ViewEncapsulation.ShadowDom,

})

the path of the input change

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-02-13 13:43:32

它嵌套在目标元素属性中。

addFile(event: any) {
    const files: File[] = event.target.files;
}

这是更干净的。直接传递输入元素:

<input #fileinput
(change)="addFile(this.fileinput)"
...

并检索它:

addFile(input: HTMLInputElement) {
    const files: FileList | null = input.files;
}

It's nested in the target element property.

addFile(event: any) {
    const files: File[] = event.target.files;
}

This is cleaner. Pass on the input element directly:

<input #fileinput
(change)="addFile(this.fileinput)"
...

and retrieve it:

addFile(input: HTMLInputElement) {
    const files: FileList | null = input.files;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文