【问题标题】:not able to fetch the attribute value from ng-bootstrap lib in angular无法从角度的 ng-bootstrap lib 中获取属性值
【发布时间】:2020-09-18 21:17:52
【问题描述】:

目前我正在使用 ng-bootstrap datepicker 并尝试从 ngb-datepicker-month 组件中获取属性值“aria-label”,但我无法直接访问 DOM,但是我尝试使用 jQuery 但它的不获取值。有没有办法在 ng-datepicker lib 中获取 attr 值?

打字稿:

    ngOnInit()
      {
        $(document).ready(() => {
    
    
    console.log('arial-label: ',$('.ngb-dp-week .hidden').get('aria-label'));
        
        function formtDate(date) {
            var d = new Date(date),
            day = <any>d.getDate();

            if(<any>day.length < 2) {
              day = day;
            }
            return day;
          }
          console.log(formtDate("Moday, October 8, 2020"));
        });
  }

HTML:

【问题讨论】:

    标签: angular ng-bootstrap


    【解决方案1】:

    您可以使用ViewChild。试试这个:

    app.component.html

    <div role="gridcell" class="ngb-dp-day hidden" aria-label="Monda, August 31, 2020" #ariaLabel></div>
    

    app.component.ts

    
    import { Component, ViewChild, ElementRef } from "@angular/core";
    
    @Component({
      selector: "my-app",
      templateUrl: "./app.component.html",
      styleUrls: ["./app.component.css"]
    })
    
    export class AppComponent {
    
      // Getting the reference
      @ViewChild(ariaLabel, { static: false }) div: ElementRef;
      ngAfterViewInit() {
         console.log( this.div.nativeElement);
      }
    }
    

    【讨论】:

    • 感谢@ng-hobby,但我的页面上有 20 多个 aria-label。我如何定位特定的?
    • 如您所见,特定 div 上有一个 templateReference `#ariaLabel`
    • 我同意这一点,但在我的情况下有些不同,因为 HTML 不可访问并且它是从 ng-bootstrap 填充的。
    • 我正在尝试获取不可编辑的组件属性
    • 是的,但是您可以将组件放在容器 div 上并使用 ViewChildren 而不是 viewChild。见这篇文章:netbasal.com/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2017-06-17
    相关资源
    最近更新 更多