【问题标题】:Wildcard in typescript打字稿中的通配符
【发布时间】:2018-03-06 23:21:37
【问题描述】:

由于我对 typescript 还很陌生,所以我想扩展一下前段时间发布的关于 typescript 中的通配符的问题。 Here is the original question.

我的 Angular 2/Web.API/MVC 应用程序正在我的数据库中使用递归搜索,以在 3 个不同的列中返回填充过滤器模式的不同类型列表。当用户在选择列表之一中选择一个值时, 我想用与第一个选择列表匹配的所有可用值填充其他 2 个选择列表。但目前,“未定义”正在从组件传递到控制器,导致它查找空值,而不是所有值。

所以这是我的组件中的强制性代码:

我的更新事件:

    UpdateAllLookups(event) {
    this.allLookups = [];
    if ("year" == event.srcElement.id) {

        this.corrCodesfilter.rgln_Year = this.getSelectedItem("year").value
    } else { this.corrCodesfilter.rgln_Year = this.corrCodesfilter.rgln_Year; }
    if ("regType" == event.srcElement.id) {

            this.corrCodesfilter.rgln_type_code = this.getSelectedItem("regType").value;
    } else { this.corrCodesfilter.rgln_type_code = this.corrCodesfilter.rgln_type_code;}

    if("valdtnCode"== event.srcElement.id){
        this.corrCodesfilter.valdtn_code = this.getSelectedItem("valdtnCode").value;
    } else { this.corrCodesfilter.valdtn_code = this.corrCodesfilter.valdtn_code;}
    this._corrCodeService.getLookupsUpdate(Global.BASE_CORRECTION_CODE_ENDPOINT + "GetLookupsUpdate?", this.corrCodesfilter)
        .subscribe(allLookups => {
            this.allLookups = allLookups;
            this.getYearLookups();
            this.getRegLookups();
            this.indLoading = false;
        }, error => this.msg = <any>error);
}  

如果我在 c# 中工作,我会通过:

   if(String.IsNullOrEmpty(this.corrCodesFilter.rgln_Year)){
   this.corrCodesFilter.rgln_Year = *;}

如果值未定义,我可以做些什么来传递通配符?我无法使用未定义的值来访问我的控制器,否则我会在那里完成。非常感谢您提供的任何帮助。

【问题讨论】:

  • 不确定我是否理解正确,但该 C# 表达式的等效项是:this.corrCodesFilter.rgln_Year = this.corrCodesFilter.rgln_Year || '*'

标签: typescript wildcard angular2-forms


【解决方案1】:

如果字段未定义,您可以执行以下操作来分配默认值:

 this.corrCodesFilter.rgln_Year = this.corrCodesFilter.rgln_Year || '*';

如果不是undefined,上述表达式将返回this.corrCodesFilter.rgln_Year;如果this.corrCodesFilter.rgln_Yearundefined,则返回'*'

【讨论】:

  • 这适用于 rgln_Year,但是因为 rgln_type_code 和 valdtn_code 的值都是数字,它不会以 '*' 作为值进行编译。有什么解决方法吗?
  • 好吧,既然您希望值是数字或字符串,您可以将类型更改为 '*'|number,例如:let ddd : '*' | number = 10; ddd = ddd || '*'; 但这取决于您如何处理这些字段跨度>
猜你喜欢
  • 2016-01-19
  • 2020-02-09
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2017-12-24
相关资源
最近更新 更多