【问题标题】:How to change Bootstrap elements' properties in Angular如何在 Angular 中更改 Bootstrap 元素的属性
【发布时间】:2021-08-25 07:28:10
【问题描述】:

我想使用 Angular 中的数据绑定更新 Bootstrap 进度条的进度条属性 aria-valuenow,但我很难确定要使用哪些命令

<div class="row">
    <div class="col-12 col-xl-3 col-lg-4 col-sm-6" *ngFor="let corso of corsi">
        <div class="card">
            <img class="card-img-top w-100 d-block" src="../assets/img/pexels-pixabay-247791.jpg">
            <div class="card-body">
                <div class="card-img-overlay">
                    <h3>{{corso.titolo}}</h3>
                </div>

                <p class="card-text pb-2 fw-bold">Avanzamento corso: {{corso.progresso}}%</p>

                <div class="progress">
                    <div class="progress-bar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 80%;"></div>
                </div>   
             
                <p class="card-text pt-3">Data scadenza: {{corso.scadenza}}</p>
            </div>
            <button class="btn btn-primary d-flex align-items-center" type="button">Vedi corso</button>
        </div>
    </div>
</div>

我尝试使用 element.getElementsByClassName 从 DOM 获取属性,因为我正在迭代模拟数组,但我不知道如何访问 HTMLCollection 数组

import { Component, OnInit } from '@angular/core';
import { CORSI } from '../../mock-corsi'

@Component({
    selector: 'app-card',
    templateUrl: './card.component.html',
    styleUrls: ['./card.component.css']
})

export class CardComponent implements OnInit {
    corsi = CORSI;

    constructor() { 
        console.log("Constructor partito")
    
        var bar = document.getElementsByClassName('progress-bar');
        console.log(bar);
    }
    ngOnInit(): void { }
}

HTMLCollection array

这是一个非常简单的应用程序,但我想了解如何访问这样的数据,谢谢。

【问题讨论】:

    标签: javascript html css angular typescript


    【解决方案1】:

    如果您只想使aria-valuenow 属性动态化,您可以使用此语法来实现。

    [attr.aria-valuenow]="valueNow" 
    

    valueNow 将在您的 .ts 文件中定义。

    您的 HTML 现在将如下所示:

    <div class="progress-bar" [attr.aria-valuenow]="valueNow" aria-valuemin="0" aria-valuemax="100" style="width: 80%;"></div>
    

    在您的.ts 文件中,您可以拥有

    valueNow : number = 80;

    【讨论】:

    • SO 想知道默认设置 onInit 的valueNow 是什么(80 可以是 100 等)
    • 有点,我想让它像 HTML 中的其他 {{code}} 片段一样动态,从 .ts 中的数组“corsi”获取数据并将其放入进度-bar div 元素。您的解决方案在 atm 上不起作用。
    • 你能在 stackblitz 中重现这个问题吗?
    • 我是 stackblitz 的新手,所以稍后我会看一下,无论如何问题仅与您在我的帖子中看到的组件有关。您是否想了解更多具体内容?感谢您的宝贵时间
    【解决方案2】:

    使用 [attr.somthing] 设置 attr 和 binding 。 设置当前值使用参考# 和getAttribute

    在 HTML 中

    <div #ref [attr.aria-valuenow]="setAttr(ref)"></div>
    

    在 TS 中

    setAttr(ref){
       return ref.getAttribute('aria-valuenow') + 20;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 2017-07-11
      • 2014-12-17
      • 2015-04-25
      • 2015-01-03
      • 1970-01-01
      • 2022-01-16
      • 2011-05-03
      相关资源
      最近更新 更多