[英]How to Bind two properties in Angular
I am trying to experiment with angular bindings, I am stuck with a weird situation.. I need to bind two properties - here 'code' and 'title' are the binding properties 我正在尝试使用角度绑定,但遇到了一种奇怪的情况。.我需要绑定两个属性-这里的“代码”和“标题”是绑定属性
<app-title-component fxFlexFill [title]="code + title"></app-title-component>
This works fine! 这样很好! I get the value of concated code and title value in the screen.
我在屏幕上获得了简明代码的值和标题值。
Now I wanted to have a static text between the two bindable properties Say, I want the result to be 现在我想在两个可绑定属性之间有一个静态文本说,我希望结果是
CodeValue - CodeTitle Value CodeValue-CodeTitle值
How to do that? 怎么做? Is there a way to do binding using string interpolation?
有没有办法使用字符串插值进行绑定?
here are two ways you can get this required result. 您可以通过以下两种方法获得所需的结果。 Starting with a very simple method mentioned within the comments.
从评论中提到的一种非常简单的方法开始。
<app-title-component fxFlexFill [title]="code + ' - ' + title"></app-title-component>
secondly you can use string interpolation looking like so. 其次,您可以像这样使用字符串插值。 Be sure to be using back ticks ( ` ) for this interpolation, else it will be ignored.
确保对该插值使用反勾号(`),否则将被忽略。
<app-title-component fxFlexFill [title]="result"></app-title-component>
within your template 在您的模板中
public get result(): string
{
return `${this.code} - ${this.title}`;
}
Here is an article looking at How Angular can deal with string interpolation . 这是一篇文章,探讨Angular如何处理字符串插值 。
You could simply define a getter in your component as follows: 您可以简单地在组件中定义一个吸气剂,如下所示:
class FooComponent {
title:string;
code:string;
get formatedTitle(){return `${this.code} - ${this.title}`;}
}
<app-title-component fxFlexFill [title]="formatedTitle"></app-title-component>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.