简体   繁体   English

如何使用angularjs编译任意模板/模型?

[英]How to compile arbitrary templates/models using angularjs?

I have a web service which returns a template eg 我有一个返回模板的Web服务,例如

Hello {{Name}}, Today is {{Yesterday}}

Then I have another website which returns a json object 然后我有另一个网站返回一个json对象

{
  "Name": "Mr. Been",
  "Yesterday": "a nice day",
  "otherdata": "unknown"
}

Is it possible to replace the keys from the template with the data from the json object? 是否可以将模板中的键替换为json对象中的数据?

The template and web service data is totally dynamic (unknown). 模板和Web服务数据是完全动态的(未知)。

Yes, you can use the $interpolate service. 是的,您可以使用$interpolate服务。 It converts an string with potential angular interpolation expressions into a function that accepts a scope object and returns the interpolated string. 它将具有潜在角度插值表达式的字符串转换为接受范围对象并返回插值字符串的函数。

You would use it like this: 您可以这样使用它:

// get the template and data to interpolate however is necessary
var template = getTemplate();
var locals = getLocals();

// perform interpolation
var result = $interpolate(template)(locals);

There are some other options you can pass to customize how the $interpolate function works, so take a look at the documentation for more details. 您可以传递其他一些选项来自定义$interpolate函数的工作方式,因此请查看文档以获取更多详细信息。

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

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