简体   繁体   English

Angular 动态字符串绑定

[英]Angular dynamic string binding

I am trying to implement one standard where all string files are from separate constant files.我正在尝试实现一种标准,其中所有字符串文件都来自单独的常量文件。 Everything works great, but facing difficult when dynamic values in string occurs.一切都很好,但是当字符串中的动态值发生时面临困难。 Kindly help me how to use the constants.请帮助我如何使用常量。

constant.ts常数.ts

export enum testPageConstants {
   SuccessMessage = 'You have created {{count}} users'
}

userPage.ts用户页面.ts

export class UserPage {
   import {testPageConstants} from '...';
   constantUIBind: typeof testPageConstants;
   count = 10;

   constructor() {
       this.constantUIBind = testPageConstants;
   }
}

userPage.html userPage.html

<p> {{constantUIBind.SuccessMessage}}</p>

Output: In HTML am receiving like 'You have created {{count}} users' but I want like 'You have created 10 users Output:在 HTML 中收到类似“您已创建 {{count}} 个用户”,但我想要“您已创建 10 个用户”

Another option is to create a pipe that will interpolate the string for you.另一种选择是创建一个 pipe 将为您插入字符串。

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "interpolate"
})
export class InterpolatePipe implements PipeTransform {
  transform(value: any, args: any) {
    value = value.replace(/{{([^}}]+)?}}/g, ($1, $2) =>
      $2.split(".").reduce((p, c) => (p ? p[c] : ""), args)
    );
    return value;
  }
}

and in the template:并在模板中:

<p>{{constantUIBind.SuccessMessage | interpolate:this}}</p>

The limitation here is that it can't interpolate objects like test.test这里的限制是它不能插入像test.test这样的对象

You can check the stackblitz here .你可以在这里查看 stackblitz。

Inspiration from: https://stackoverflow.com/a/45235190/5613720灵感来自: https://stackoverflow.com/a/45235190/5613720

I am not sure if that is possible using enums though one possible workaround could be yby wrapping your literals into functions so change your enum to class like我不确定这是否可以使用enums ,尽管一种可能的解决方法可能是将您的文字包装成函数,因此将您的枚举更改为 class 之类

 class testPageConstants {
     static  SuccessMessage = (count)=>`You have created ${count} users`
  }

then create a function to render like然后创建一个 function 来渲染

 render(template, data) {
       return template(data);
   }

then on ui然后在ui上

<p> {{render(constantUIBind.SuccessMessage,count)}}</p>

demo演示

Would the following work for you?以下内容对您有用吗?

import { Component } from '@angular/core';

export class TestPageConstants {
   static SuccessMessage = (count) => { return `You have created ${count} users`};
}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
   constantUIBind: typeof TestPageConstants;
   count = 10;

   constructor() {
       this.constantUIBind = TestPageConstants;
   }
}

UI用户界面

<p> {{constantUIBind.SuccessMessage(count)}}</p>

Check stackblitz here在此处检查堆栈闪电战

For this you usually use the TranslateService , where you define several strings through out your application.为此,您通常使用TranslateService ,您可以在整个应用程序中定义多个字符串。 Later you can use it like that and have the functionality to add parameters to it like:稍后您可以像这样使用它并具有向其添加参数的功能,例如:

<div>{{ 'SuccessMessage' | translate:param }}</div>

SuccessMessage is your constant to get from a dictionary and param is the parameter like you mentioned. SuccessMessage是您从字典中获取的常量,而param是您提到的参数。

What do you need to do?你需要做什么?

1. Install Ngx Translate and init the TranslateService for your application 1. 安装Ngx Translate并为您的应用程序初始化 TranslateService

2. Define the translations/string constants 2.定义翻译/字符串常量

Once you've imported the TranslateModule, you can put your translations in a json file that will be imported with the TranslateHttpLoader.导入 TranslateModule 后,您可以将翻译放入 json 文件中,该文件将使用 TranslateHttpLoader 导入。 The following translations should be stored in en.json.以下翻译应存储在 en.json 中。

{
    "HELLO": "hello {{value}}"
}
  1. Use the service, the pipe or the directive使用服务、pipe 或指令
    • With the service, it looks like this:使用该服务,它看起来像这样:
translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
    console.log(res);
    //=> 'hello world'
});
  • This is how you do it with the pipe:这是使用 pipe 的方法:
<div>{{ 'HELLO' | translate:{value: 'world'} }}</div>

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

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