简体   繁体   English

从本地存储中获取数据并使用 ngx-translate 将其显示在视图中 | Angular

[英]Get data from localstorage and display it in view using ngx-translate | Angular

I have a task have to display data from localstorage in html using ngx-translate.我有一个任务必须使用 ngx-translate 在 html 中显示来自本地存储的数据。 I have an backend spring-boot application with a controller that sends data downloaded from the database to the frontend.我有一个后端 spring-boot 应用程序和一个 controller 将从数据库下载的数据发送到前端。 This data is stored in localstorage.此数据存储在本地存储中。 They are literals.它们是文字。 I have little in common with Angular.. I work in Java, but I need to do it:/ I am asking for your understanding.我与 Angular 几乎没有共同之处。我在 Java 工作,但我需要这样做:/ 我请求您的理解。

localstorage:

          key            |          value
--------------------------------------------------
Platform.Title           |         "Title"
Platform.Home            |         "Home"
Platform.Dashboard       |         "Dashboard"
Library.Title            |         "Library"
Library.Home             |         "Home"
Library.Dashboard        |         "Dashboard"

This what i need to do is: make service to get data from localstorage, i know i have to do it with我需要做的是:提供服务以从本地存储中获取数据,我知道我必须这样做

localstorage.get(key);

What I need to achieve is eq我需要实现的是 eq

<mat-card-title>{{'Platform.Title' | translate }}</mat-card-title>

and this should show value of "Platform.Title" - here "Title"这应该显示“Platform.Title”的值 - 这里是“Title”

I have keys which starts with different values Platform or Library.我有以不同值平台或库开头的键。 In platform.component.html i need to display all starts with "Platform" and in library.component.html all with "Library"在platform.component.html中,我需要显示所有以“Platform”开头的内容,在library.component.html中,所有内容都以“Library”开头

My ideas so far have not worked, it does not even show because it is a shame.到目前为止,我的想法还没有奏效,它甚至没有表现出来,因为它是一种耻辱。 I have no idea how should servis in Angular looks like or some ngx-translate loader to achieve the planned effect.我不知道 Angular 中的 servis 应该如何或一些 ngx-translate 加载程序来达到计划的效果。 Could someone please help me?有人可以帮我吗? Show the way to a solution or maybe a sample solution.显示解决方案或示例解决方案的方法。

you can get your data from local storage in this way您可以通过这种方式从本地存储中获取数据

in your component.ts在你的 component.ts

public title;

ngOnInit(){
  this.title = localStorage.getItem('KEY');
}

in your HTML在你的 HTML

<mat-card-title>{{title | translate }}</mat-card-title>

in your app module add在您的应用模块中添加

export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);}

in your app component add在您的应用组件中添加

 constructor(private translate: TranslateService) {
    translate.setDefaultLang('en');
}

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

相关问题 在 Angular 4+ 中使用 ngx-translate 进行本地化(国际化)? - Localization (internationalization) in Angular 4+ using ngx-translate? 如何从 Angular 6 中的组件中提取文本进行翻译 (ngx-translate) - How to extract text from components in Angular 6 for translation (ngx-translate) ngx-translate 在 angular 8 中的 terinory 条件下 - ngx-translate in terinory condtion in angular 8 Angular 4 Ngx-translate管不起作用 - Angular 4 Ngx-translate pipe not working 使用组件作为 ngx-translate 键的参数 - Using a component as a parameter for a ngx-translate key 如何使用参数动态翻译文本文件到 angular 中的 ngx-translate? - How to dynamic translate text file using parameter to the ngx-translate in angular? Angular ngx-translate - 如何为翻译值中缺少的参数设置默认值 - Angular ngx-translate - How to set a default value for missing param in translation value 使用本地存储从表单发布和获取数据 - Post and get data from form using localstorage 如何使用javascript或jQuery在html视图页面中显示LocalStorage数据? - How to display LocalStorage data in html view page on refresh using javascript or jQuery? 从 localstorage 中的数组中获取项目并使用按钮单独显示它们 - Get items from an array in localstorage and display them each individually using a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM