简体   繁体   English

Angular2默认过滤器用于未初始化的值

[英]Angular2 default filter for uninitialized values

Is there a way to substitute default values for component variable bindings in the HTML tags? 有没有办法用默认值替代HTML标记中的组件变量绑定? For example, currently I use code like so: Some text with data binding ="{{searchResult.title || "n/a"}}" not found. 例如,当前我使用这样的代码:找不到一些带有数据绑定=“ {{searchResult.title ||” n / a“}}”“的文本。

However I don't like this coz when you have 20 fields adding '|| 但是,当您有20个字段添加'||时,我不喜欢这个原因。 "n/a"' for each and every one of them becomes a little tedious. 每个人的“ n / a”'变得有点乏味。

You can set your model to "n/a" by default. 您可以默认将模型设置为“ n / a”。

public modelObject:modelType = new Model({});

constructor(){
   let self = this;
   Object.keys(this.modelObject).forEach(keyElement => {
     self.modelObject[keyElement] = "n/a";
   });
}

This will set a default without having it in your binding. 这将设置默认值,而无需绑定。 And will be changed when the data is present/overwritten. 存在/覆盖数据时将更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM