简体   繁体   English

如何获取 I18n 变量值我可以返回到我的 Angular 父组件?

[英]How to get a I18n variable value I can return to my Angular parent component?

I'm new to Angular and I just put in place an i18n (2 languages) system for a website I am creating.我是 Angular 的新手,我刚刚为我正在创建的网站安装了 i18n(2 种语言)系统。 Everything works properly but in order to switch from one language to another in my header, I feel stuck.一切正常,但为了在我的 header 中从一种语言切换到另一种语言,我觉得卡住了。

I followed the Angular documentation to transfer my variables from child to parent component and I ended with this:我按照 Angular 文档将我的变量从子组件传输到父组件,并以此结束:

<input type="text" id="item-input" #lang>
  <button type="button" (click)="changeChosenLang(lang.value)">
    {{ 'global.lang' | translate }}
  </button>

As you can see, I write my language in the input form and I send it to the proper component with a button.如您所见,我在输入表单中编写我的语言,然后通过按钮将其发送到适当的组件。 What I wanted was to click on my 'global.lang' text and to be able to send its value to the parent component, since the value is the language which is not actually used.我想要的是单击我的“global.lang”文本并能够将其值发送到父组件,因为该值是实际未使用的语言。

I don't know how to put my 'global.lang' text in a variable, neither what kind of balise I can use.我不知道如何将我的“global.lang”文本放入变量中,也不知道我可以使用什么样的应答器。 Also I didn't know how to summarize my problem to search for it on StackOverflow so if you know a similar post, don't hesitate to post the link.另外我不知道如何总结我的问题以在 StackOverflow 上搜索它,所以如果您知道类似的帖子,请不要犹豫发布链接。

Thank you for your reading!感谢您的阅读!

I found a less tortured way (poor brain) to have the result I wanted:我找到了一种不那么折磨人的方式(可怜的大脑)来获得我想要的结果:

<span (click)="changeChosenLang()">
    {{ 'global.lang' | translate }}
  </span>

First I temporary changed my button to a span balise and I deleted the parameter from my changeChosenLang() function.首先,我临时将按钮更改为跨度应答器,并从 changeChosenLang() function 中删除了参数。 Then, I transferred a variable 'lang' from my parent component to this one, witch contains the value of the language chosen in my app constructor.然后,我将一个变量“lang”从我的父组件转移到这个组件,其中包含在我的应用程序构造函数中选择的语言的值。 At each click, I change its value in my changeChosenLang() function and everything works great!每次单击时,我都会在 changeChosenLang() function 中更改其值,一切正常!

I hope it can help someone someday.我希望有一天它可以帮助某人。 The moral of this post is: the simpler, the better.这篇文章的寓意是:越简单越好。 Have a good day.祝你有美好的一天。

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

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