Angular4和data

编程入门 行业动态 更新时间:2024-10-26 02:26:53
Angular4和data- *属性在嵌套Component中注入变量(Angular4 and data-* attributes with injected variable in nested Component)

在Angular4的嵌套组件内部有以下代码:

<a data-activator="classroom-panel-activator" data-toggle="collapse" data-parent="#accordion-{{ day.date }}" href="#info-panel-{{ schedule.referenceId }}" > Click me </a>

问题在于data-parent及其{{ day.date }}值,因为当我在浏览器中运行代码时,Angular会抛出以下内容:

Can't bind to 'parent' since it isn't a known property of 'a'. (" <a data-activator="classroom-panel-activator" data-toggle="collapse" [ERROR ->]data-parent="#accordion-{{ day.date }}" href="#info-panel-{{ schedule.referenceId }}"

当变量注入data-*属性时,问题确实发生。 如果我{{ day.date }}删除{{ day.date }} ,那么它就可以了。 此外,如果我离开{{ day.date }}并且例如将名称从data-parent更改为data-nothing那么错误仍然存​​在(因此它与任何关键字parent没有名称冲突)。

当然{{ day.date }}对象存在并且有效。 它只是在我描述的场景中不起作用。

那么问题是什么呢?

Inside the nested Component of Angular4 have the following piece of code:

<a data-activator="classroom-panel-activator" data-toggle="collapse" data-parent="#accordion-{{ day.date }}" href="#info-panel-{{ schedule.referenceId }}" > Click me </a>

The problem is with data-parent and its value of {{ day.date }} because when I ran the code in the browser, Angular throws the following:

Can't bind to 'parent' since it isn't a known property of 'a'. (" <a data-activator="classroom-panel-activator" data-toggle="collapse" [ERROR ->]data-parent="#accordion-{{ day.date }}" href="#info-panel-{{ schedule.referenceId }}"

The problem really occurrs when the variable is injected into a data-* attribute. If I remove {{ day.date }} from it, then it works. Also, if I leave the {{ day.date }} and e.g. change the name from data-parent to data-nothing then error still occurrs (so it's not kind of name conflict with any keyword parent).

Of course {{ day.date }} object exists and works. It's just does not work in the scenario I have described.

So what is the problem then?

最满意答案

您想使用属性绑定

<a data-activator="classroom-panel-activator" data-toggle="collapse" [attr.data-parent]="'#accordion-' + day.date" href="#info-panel-{{ schedule.referenceId }}" > Click me </a>

You want to use attribute binding

<a data-activator="classroom-panel-activator" data-toggle="collapse" [attr.data-parent]="'#accordion-' + day.date" href="#info-panel-{{ schedule.referenceId }}" > Click me </a>

更多推荐

本文发布于:2023-07-05 07:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!