简体   繁体   English

如何将数据绑定到 Angular4 中的 html 属性?

[英]How can I bind data to the html attribute in Angular4?

I am using the *ngFor directive to populate a series of divs.我正在使用 *ngFor 指令来填充一系列 div。 I am also using the bootstrap library for css.我也在使用 css 的 bootstrap 库。

<div data-toggle="collapse" data-target="#toggleDemo"*ngFor="let pair of pairings">
    <div data-target="#toggleDemo">
    </div>
</div>

I am going to have a number of these entries and I want to use expand/collapse from bootstrap.我将有许多这样的条目,我想使用 bootstrap 中的展开/折叠。 In order to get this to work, each div must have a unique data-target attribute value.为了让它起作用,每个 div 必须有一个唯一的 data-target 属性值。

I want to use one of the properties from my 'pair' object but each time I try it, I get an error.我想使用我的“配对”对象中的一个属性,但每次尝试时,我都会收到错误消息。

Is it possible to do something like this in Angular4?在 Angular4 中可以做这样的事情吗?

<div data-target="#toggleDemo-"{{ pair.id }}>

Edit:编辑:

I have tried this approach我试过这种方法

<div data-toggle="collapse" data-target="#toggleDemo-{{ pair.id }}">

and I get this error:我收到这个错误:

Template parse errors:
Can't bind to 'target' since it isn't a known property of 'div'. ("v class="row" *ngFor="let pair of pairings">
            <div class="wall col-sm-2" data-toggle="collapse" [ERROR ->]data-target="#toggleDemo-{{ pair.buyer.phone }}">
                <p>{{ pair.buyer.name.first }} {{ pair.buyer.na"): ng:///AppModule/PairingsComponent.html@4:52 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:
    Can't bind to 'target' since it isn't a known property of 'div'. ("v class="row" *ngFor="let pair of pairings">

Edit:编辑:

I have tried what the answers suggest but none of the solutions worked out.我已经尝试了答案所暗示的,但没有一个解决方案奏效。

Angular by default does property binding. Angular 默认进行属性绑定。 In your case you need attribute binding because that property is not reflected to an attribute automatically:在您的情况下,您需要属性绑定,因为该属性不会自动反映到属性:

<div attr.data-target="#toggleDemo-{{ pair.id }}">

or或者

<div [attr.data-target]="'#toggleDemo-' + pair.id">

当然,只需在那里的字符串中包含插值和花括号。

<div data-target="#toggleDemo-{{ pair.id }}">

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

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