【问题标题】:How do I pass a value from *ngFor (Angular) to the data-target attribute?如何将 *ngFor (Angular) 中的值传递给 data-target 属性?
【发布时间】:2020-11-30 02:06:33
【问题描述】:

我想使用 Angular 和 Bootstrap 库制作动态可折叠列表。因为我不知道会有多少,所以我想把它们放在*ngFor 循环中。我一开始就遇到了问题。我需要知道我正在折叠和隐藏什么元素。为此,有两个参数data-target(用于按钮)和id 用于文本列表。我下面的方法不起作用。我收到一个错误:

ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'target' since it isn't a known property of 'button'. ("One">
          <h5 class="mb-0">
            <button class="btn btn-link" data-toggle="collapse" [ERROR ->]data-target="{{section.key}}" aria-expanded="true" aria-controls="collapseOne">
              {{sect"): ng:///PortalModule/FaqComponent.html@14:64
Error: Template parse errors:
Can't bind to 'target' since it isn't a known property of 'button'. ("One">
          <h5 class="mb-0">
            <button class="btn btn-link" data-toggle="collapse" [ERROR ->]data-target="{{section.key}}" aria-expanded="true" aria-controls="collapseOne">
              {{sect"): ng:///PortalModule/FaqComponent.html@14:64
    at syntaxError (compiler.js:2175)
    at TemplateParser.parse (compiler.js:11388)
    at JitCompiler._parseTemplate (compiler.js:25961)
    at JitCompiler._compileTemplate (compiler.js:25949)
    at compiler.js:25893
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:25893)
    at compiler.js:25806
    at Object.then (compiler.js:2166)
    at JitCompiler._compileModuleAndComponents (compiler.js:25805)
    at resolvePromise (zone-evergreen.js:797)
    at resolvePromise (zone-evergreen.js:754)
    at zone-evergreen.js:858
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:39680)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)
    at drainMicroTaskQueue (zone-evergreen.js:559)

这是我的代码。

  <div id="accordion">
    <div *ngFor="let section of sections; let i = index">
      <div class="card">
        <div class="card-header" id="headingOne">
          <h5 class="mb-0">
            <button class="btn btn-link" data-toggle="collapse" data-target="{{section.key}}" aria-expanded="true" aria-controls="collapseOne">
              {{section.value}}
            </button>
          </h5>
        </div>
        <div id="{{section.key}}" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
          <div class="card-body">
            Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
          </div>
        </div>
      </div>
    </div>
  </div>

问题在于这两个参数。我不知道如何动态分配它们。

【问题讨论】:

    标签: html angular bootstrap-4


    【解决方案1】:

    Here 是可能有帮助的答案。

    [attr.data-target]="value"

    【讨论】:

      猜你喜欢
      • 2016-06-26
      • 2018-06-13
      • 2017-08-17
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多