Angular 如何在角度视图中绑定两个属性
我正在尝试角度绑定,我被一个奇怪的情况困住了。。我需要绑定两个属性——这里的“code”和“title”是绑定属性Angular 如何在角度视图中绑定两个属性,angular,angular6,angular7,Angular,Angular6,Angular7,我正在尝试角度绑定,我被一个奇怪的情况困住了。。我需要绑定两个属性——这里的“code”和“title”是绑定属性 <app-title-component fxFlexFill [title]="code + title"></app-title-component> 这个很好用! 我在屏幕上得到了具体代码的值和标题值 现在我想在两个可绑定属性之间有一个静态文本 说吧,我希望结果是 CodeValue-CodeTitle值 怎么做?有没有一种方法可以使用字符串插值
<app-title-component fxFlexFill [title]="code + title"></app-title-component>
这个很好用!
我在屏幕上得到了具体代码的值和标题值
现在我想在两个可绑定属性之间有一个静态文本
说吧,我希望结果是
CodeValue-CodeTitle值
怎么做?有没有一种方法可以使用字符串插值进行绑定?这里有两种方法可以获得所需的结果。从评论中提到的一个非常简单的方法开始
<app-title-component fxFlexFill [title]="code + ' - ' + title"></app-title-component>
.您只需在组件中定义一个getter,如下所示:
class FooComponent {
title:string;
code:string;
get formatedTitle(){return `${this.code} - ${this.title}`;}
}
<app-title-component fxFlexFill [title]="formatedTitle"></app-title-component>
class组件{
标题:字符串;
代码:字符串;
get FormattedTitle(){return`${this.code}-${this.title}`;}
}
在组件中定义一个getter,比在模板中嵌入字符串逻辑更干净。对不起,编辑失败了,跳枪了。谢谢你指出这一点:-)
public get result(): string
{
return `${this.code} - ${this.title}`;
}
class FooComponent {
title:string;
code:string;
get formatedTitle(){return `${this.code} - ${this.title}`;}
}
<app-title-component fxFlexFill [title]="formatedTitle"></app-title-component>