gpt4 book ai didi

angular - 如何将多个属性绑定(bind)到 Angular 5 组件?

转载 作者:行者123 更新时间:2023-12-04 12:25:53 26 4
gpt4 key购买 nike

我有一个组件 toolbar带模板:

<div *ngFor="let item of items" class="toolbar__item">
<a href="{{ item.url }}" [attributes]="item.attributes">{{ item.label }}</a>
</div>

我想绑定(bind)一个数组 item.optionsA元素。如何使用 Angular 5 做到这一点?
const items = [
{ 'url': 'someurl', 'label': 'Label', 'attributes': {'data-target': '#dropdown', 'data-toggle': 'dropdown'} }
]

最佳答案

我希望您可以创建一个指令来存储动态属性并将其分配给元素,但我尚未对其进行测试。

import {Directive, ElementRef, Input} from '@angular/core';

@Directive({
selector: '[dynamicAttributes]'
})
export class DynamicAttributesDirective {
@Input() dynamicAttributes: {[key: string]: string};

constructor(public element: ElementRef<HTMLElement>) {}

ngOnInit() {
Object.assign(this.element.nativeElement.attributes, this.dynamicAttributes)
}
}

你会按如下方式消费它
<a href="{{item.url}}" [dynamicAttributes]="item.attributes">{{item.label}}</a>

关于angular - 如何将多个属性绑定(bind)到 Angular 5 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49768116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com