[英]<%= %> syntax in Typescript/Angular
I'm working on an Angular project that makes use of a variable called WEB_HOST
. 我正在使用一个名为
WEB_HOST
的变量的Angular项目。 I can see in another file where the variable is defined (it's somehow defined in a class called ProjectConfig
). 我可以在另一个文件中看到定义了变量的地方(以某种方式在名为
ProjectConfig
的类中进行了定义)。
Definition (simplified): 定义(简体):
export class ProjectConfig {
WEB_HOST = 'https://example.com'; // I didn't think properties could be assigned here?
constructor() { ... }
}
Now, from a completely separate *.ts file, this obviously doesn't work: 现在,从一个完全独立的* .ts文件,这显然不起作用:
let x = WEB_HOST; // x is undefined
However, in the same file, some existing code manages to access the variable's value by using a bizarre string interpolation: 但是,在同一文件中,一些现有代码通过使用奇异的字符串插值来设法访问变量的值:
let x = `<%= WEB_HOST %>`; // x == 'https://example.com'
What is going on here? 这里发生了什么? I've never seen this
<%=
syntax before. 我以前从未见过这种
<%=
语法。 Is it part of Angular? 它是Angular的一部分吗? Typescript?
打字稿? I haven't been able to find any documentation on what it does.
我尚未找到有关其功能的任何文档。
I also don't know how it manages to get a property out of ProjectConfig
(and I'm not sure it's a class property either... the definition being outside of any class function confuses me too). 我也不知道它是如何设法从
ProjectConfig
获取属性的(并且我也不确定这是否是类属性...任何类函数之外的定义也使我感到困惑)。
So, this is ASP Tags. 因此,这是ASP标记。
If you take a look at the following from w3schools. 如果您从w3schools看以下内容。 https://www.w3schools.com/asp/showasp.asp?filename=demo_syntax_tags
https://www.w3schools.com/asp/showasp.asp?filename=demo_syntax_tags
You will see that it is used in the HTML for templating. 您将看到它在HTML中用于模板化。 It can also be used I believe in other files as it is processed on the server before the client application runs on your browser.
我相信它也可以用于其他文件,因为在客户端应用程序在您的浏览器上运行之前,它已经在服务器上进行了处理。
HTH 高温超导
Looks like ERB, a Ruby templating engine that Ruby on Rails uses. 看起来像ERB,这是Ruby on Rails使用的Ruby模板引擎。 https://codingbee.net/tutorials/ruby/ruby-the-erb-templating-system
https://codingbee.net/tutorials/ruby/ruby-the-erb-templating-system
Others have mentioned that various languages use <%= some_code_here %>
for templating. 其他人提到各种语言使用
<%= some_code_here %>
进行模板制作。
Regardless of what language is processing it, I imagine your .ts file runs through some server side framework, that will put some string value in place of WEB_HOST, and then the .ts file will be transpiled to JS. 不管使用什么语言处理它,我都可以想象您的.ts文件通过某个服务器端框架运行,它将在WEB_HOST位置放置一些字符串值,然后将该.ts文件转换为JS。
You could try this way on HTML file 您可以在HTML文件上尝试这种方式
{{window.location.origin}} {{window.location.origin}}
if it doesn't recognize the window variable we might need this in ts file 如果无法识别window变量,我们可能需要在ts文件中使用
declare const window; 声明常量窗口;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.