angular - 如何从 ReactivForms 中的 FormControl 返回图像名称

标签 angular typescript

我想从 Angular ReactivForms 中图像的 FormControl 获取名称。我获得了图像的完整路径,例如 file = "C:\fakepath\Screenshot (1).png" 但我只想要 Screenshot (1).png。 我使用 control.value 来获取图像的名称。 我想添加我的 C: 目录中没有此文件夹 C:\fakepath\

这是我的方法:

 requiredFileType(categoryList: CategoryModel[]) {
    return function (control: FormControl) {
      const file = control.value;
      if (file) {
        const extension = file;

        for (let i = 0; i < categoryList.length; i++) {

          for (let j = 0; j < categoryList[i].productModel.length; j++) {
            for (let k = 0; k < categoryList[i].productModel[j].imageProduct.length; k++) {
              console.log(categoryList[i].productModel[j].imageProduct[k].image_path);
              if (categoryList[i].productModel[j].imageProduct[k].image_path.includes(extension)) {
                console.log(categoryList[i].productModel[j].imageProduct[k].image_path);

                return{
                  requiredFileType: true
                };

              }
            }

          }

        }
        return null;

      }

      return null;

    };
  }

有谁知道这个问题的解决办法吗?

最佳答案

如果您的路径具有静态字符数(不变),请使用substring:

const file = control.value;
const fileName = file.substring(10, str.length);

演示:

let str = "C:\fakepath\Screenshot (1).png";
const mystr = str.substring(10, str.length);
console.log(mystr)

关于angular - 如何从 ReactivForms 中的 FormControl 返回图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691376/

相关文章:

dependencies - typescript 中有没有什么好的静态代码分析工具?

javascript - 你如何链接 promise 但将类型从一个然后更改为另一个?

angular - 如何使用 Angular Material 渲染没有 id 的表格

javascript - 为什么我的 Framer Motion 动画发生两次?

javascript - 在 ES6 之前的 Typescript 中实现 Iterator<T> 的推荐方法

angular - TS2322 : Type 'HTMLDivElement' is not assignable to type 'TemplateRef<NgIfContext<Skill[]>>'

angular - Angular 5中的嵌套路由

angular - 为什么我不能构建引用内部 Angular 库的 Angular 应用程序?

angular - 嵌入式模板上的任何指令均未使用属性绑定(bind) matHeaderRowDef

javascript - Angular - DOM .contains() 找不到元素且 ParentNode 为 null