值的插值作为属性Component [英] Interpolation of the value as the property Component

查看:60
本文介绍了值的插值作为属性Component的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,关于如何在Angular2中进行插值.例如,我有X组件,其值为@Input();在父组件中,我有这样的代码:

I have a question about how is the interpolation in the Angular2 done. For example, I have the component X, that have value as @Input(); And in the parent component i have such code:

<X [value]="{{'hello' | translate}} {{ 'world' | translate }}"></X>

但是据我判断,这样的代码是错误的.我使用ng2-translate进行国际化.您能否提供信息链接给我,鞭子可以帮助我吗?

But as I can judge such code is wrong. I use ng2-translate for internationalization. Can you provide me withe the links of information, whick can help me?

推荐答案

如果在子组件中定义了 @Input()some ,则有4个选项:

If you defined an @Input() some in the child component, you have 4 options:

  1. 请勿传递任何内容-输入内容将不确定

<子组件... -表达式将被解析为未定义,而 this.some 将等于未定义

<child-component ... - expression will be parsed as undefined and this.some will be equal to undefined

  1. 既不使用插值法也不使用绑定语法-

<子组件some ="value" ... -表达式将被解析为字符串,而 this.some 将等于 value 字符串;

<child-component some="value" ... - expression will be parsed a string and this.some will be equal to value string;

  1. 使用插值-<子组件some ="{{value}}" ...>
  2. 使用绑定语法-< child-component [some] ="value" ...>

第3个和第4个选项都会产生完全相同的 updateBindings函数.该表达式将在父组件的上下文中求值,并且无论父组件的 value 属性如何,输入的 this.some 都将相等.

Both 3-rd and 4-th option will produce exactly the same updateBindings function in the component factory. The expression will be evaluated in the context of the parent component and the input this.some will be equal whatever the value property of the parent component holds.

请参阅模板语法手册中的更多内容.

在您的情况下,插值似乎很好:

It seems that in your case the interpolation will do just fine:

<X value="{{'hello' | translate}} {{ 'world' | translate }}"></X>

这篇关于值的插值作为属性Component的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆