简体   繁体   English

Angular:将属性传递给动态组件模板

[英]Angular: passing attributes to Dynamic component template

I have a component created, and I want to pass an attribute though the component to the template. 我创建了一个组件,我想通过组件将属性传递给模板。 This is the component: 这是组件:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-card-generator',
  templateUrl: './card-generator.component.html',
  styleUrls: ['./card-generator.component.css'],
  inputs: ['id', 'collapseid', 'headingid','collapsehashid']
  })
export class CardGeneratorComponent implements OnInit {
  bindings: {
            headingid:'@?',
    collapseid:'@?',
    collapsehashid
           }
  constructor() { }
  ngOnInit() {}

and this is the template: 这是模板:

<button class="btn btn-link collapsed"  style="text-decoration:none;" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          {{id}}
        </button>
      </h5>
    </div>
    <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
      <div class="card-body"></div>
    </div>

created components are placed in home.component this way: 创建的组件以这种方式放在home.component中:

<div id="cardDivContainer" >

    <app-card-generator id="Chart 1" collapsehashid="data-target='#collapseOne'" collapseid="aria-controls='collapseOne'" headingid="aria-labelledby='headingOne'"></app-card-generator>
    <app-card-generator id="Chart 2" collapsehashid="data-target='#collapseTwo'" collapseid="aria-controls='collapseTwo'" headingid="aria-labelledby='headingTwo'"></app-card-generator>

  </div>

I just need to set the "data-target", "aria-labelledby", and the "aria-controls" attributes for each component (depending on the component ID). 我只需要为每个组件设置“data-target”,“aria-labelledby”和“aria-controls”属性(取决于组件ID)。

I am new to Angular, I hope the above make sense. 我是Angular的新手,我希望上面有意义。

You can use @Input to reference the elements within the component. 您可以使用@Input引用组件中的元素。 The elements can be passed as parameter outside. 元素可以作为参数传递到外部。 Something like: 就像是:

import { 
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter
 } from '@angular/core';
@Component({
  selector: 'app-card-generator',
  styleUrls: ['./card-generator.component.css'],
  templateUrl: './card-generator.component.html'
  })
export class CardGeneratorComponent implements OnInit {
  @Input() id: string;
  @Input() collapseid: string;
  @Input() headingid: string;
  @Input() collapsehashid: string;

  constructor() {
  }

  ngOnInit() {}
}

If the attributes do not exist in the element you are referencing, you can use attr.attribute and use the {{}} notation 如果要引用的元素中不存在这些属性,则可以使用attr.attribute并使用{{}}表示法

<button class="btn btn-link collapsed"  style="text-decoration:none;" type="button" data-toggle="collapse" attr.data-target="{{'#' + collapseid}}" aria-expanded="false" attr.aria-controls="collapseid">

        </button>
    <div id="{{collapseid}}" class="collapse" attr.aria-labelledby="{{headingid}}" data-parent="#accordionExample">
      <div class="card-body"></div>
    </div>

And finally you can access the attributes created in your component in the call from the outside 最后,您可以从外部访问调用中组件中创建的属性

<div id="cardDivContainer" >

    <app-card-generator id="Chart 1" collapsehashid="collapseOne" headingid="headingOne"></app-card-generator>
    <app-card-generator id="Chart 2" collapsehashid="collapseTwo" headingid="headingTwo"></app-card-generator>

  </div>

More @Input and @Output details in this link: 此链接中的更多@Input@Output详细信息:

https://stackblitz.com/edit/card-generator-sample?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/card-generator-sample?file=src%2Fapp%2Fapp.component.html

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

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