简体   繁体   English

有没有办法在Angular 4中使用“模板引用变量”?

[英]Is there a way to use a “template reference variable” with Angular 4?

I am trying to figure how to use a template reference variable in a .pug template. 我试图找出如何在.pug模板中使用模板引用变量。

For example: div(#var) will throw an error: 例如: div(#var)将抛出一个错误:

compiler.es5.js:1694 Uncaught Error: Template parse errors: There is no directive with "exportAs" set to "#var" (" ... 

The cause is that pug will render as : 原因是哈巴狗会呈现为:

<div #var="#var"> ...

Angular will then fail. 然后Angular会失败。

From the doc : 来自doc

Template reference variables ( #var ) 模板引用变量(#var)

A template reference variable is often a reference to a DOM element within a template. 模板引用变量通常是对模板中DOM元素的引用。 It can also be a reference to an Angular component or directive or a web component. 它也可以是对Angular组件或指令或Web组件的引用。

So, you just need <div #var > ,The #var declares a var variable on this <div> element. 所以,你只需要<div #var > ,# var在这个<div>元素上声明一个var变量。

In most cases, Angular sets the reference variable's value to the element on which it was declared.... But a directive can change that behavior and set the value to something else, such as itself. 在大多数情况下,Angular将引用变量的值设置为声明它的元素.... 但是指令可以更改该行为并将值设置为其他值,例如它自己。 The NgForm directive does that NgForm指令就是这样做的

If you assign something in the template ref variable, it should be a directive or a component , for example: #var="ngForm" which ngForm is a built-in directive. 如果在模板引用变量中指定了某些内容,则它应该是指令组件 ,例如: #var="ngForm"哪个ngForm是内置指令。

So that's why you get an error: There is no directive with "exportAs" set to "#var" 这就是为什么你得到一个错误: There is no directive with "exportAs" set to "#var"

Because #var (you assigned with: <div #var="#var"> ) is not a component nor a directive, 因为#var (您指定的: <div #var="#var"> )不是组件也不是指令,

Now for jade (pug), if you want a null attribute,you should set the compiler to compile to HTML doctype because the default behaviour of pug is to set an attribute value which is the same name on the attribute: 现在对于jade(pug),如果你想要一个null属性,你应该将编译器设置为编译为HTML doctype,因为pug的默认行为是设置一个属性值,该属性值与属性上的名称相同:

default behaviour: 默认行为:

div(#var) compiled to: <div #var="#var"></div> div(#var)编译为: <div #var="#var"></div>

div(hidden) compiled to <div hidden="hidden"></div> div(hidden)编译为<div hidden="hidden"></div>

with doctype html : 使用doctype html

div(#var) compiled to: <div #var></div> div(#var)编译为: <div #var></div>

div(hidden) compiled to <div hidden></div> div(hidden)编译为<div hidden></div>

Or you can just put in beginning of the file: doctype html for each file you want this. 或者你可以把文件的开头: doctype html放到你想要的每个文件中。

I had the same case and was looking how to solve it. 我有同样的情况,正在寻找如何解决它。

I found this solution: 我发现这个解决方案:

Example: div('#refMyDiv'='') 示例: div('#refMyDiv'='')

Resource: https://medium.com/paramsingh-66174/pugjs-primer-for-your-next-angular-4-project-409ee6696fad 资源: https//medium.com/paramsingh-66174/pugjs-primer-for-your-next-angular-4-project-409ee6696fad

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

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