admin管理员组文章数量:1333451
What is the best practice for string interpolation in attribute in Angular 6?
I have this code:
<div class="container" [ngStyle]="{'grid-template-rows': 'repeat(' + value + ', 1fr) [last-line]'}">
I want to use something like 'repeat(${value})'
with backtick
What is the best practice for string interpolation in attribute in Angular 6?
I have this code:
<div class="container" [ngStyle]="{'grid-template-rows': 'repeat(' + value + ', 1fr) [last-line]'}">
I want to use something like 'repeat(${value})'
with backtick
2 Answers
Reset to default 6You can move the functionality to your ponent and use backticks there:
calculateStyle(value: string): string {
return `repeat(${value}, 1fr) [last-line]`;
}
and in template:
<div class="container" [ngStyle]="{'grid-template-rows': calculateStyle(value)}">
However you should try to avoid calling function from template whenever possible, so consider using some technique to avoid that (e.g. having observable remapped from an input, using state management, ...)
I came here looking for answer to the question, and while Mauricio expresses opinion based on article about using methods in templates, I think this is fine on a case-by-case basis. Hence, my upvote on the original answer.
However, I would like to build a string from logic in the template - like the question states. In my test on ver 14+, concatenating is possible with the double braces. For example:
<someComponent
someProperty="{{someObj?.anotherProp}}{{someVar ? ' [TEST]' : null}}">
</someComponent>
Note the omission of the square brackets on the property name.
本文标签: javascriptAngular String interpolation in attribute templateStack Overflow
版权声明:本文标题:javascript - Angular String interpolation in attribute template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742351053a2458499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论