簡體   English   中英

我可以使用角度組件選擇器作為該組件的屬性嗎?

[英]Can I use an angular component selector as an attribute for that component?

例如,

    @Component({
      selector: 'editor', //Same as component input
      templateUrl: './editor.component.html',
      styleUrls: ['./editor.component.scss']
    })
    export class Editor implements OnInit {
      @Input() editor: string; //Same name as selector
      @Input() color: string;
      constructor() { }

      ngOnInit() {
      }
   }

HTML: <div editor="value" color="blue"></div>

到目前為止,我的經驗是這行不通。 有人對如何進行這項工作有任何想法嗎? 還是有可能?

您應該在div標簽上使用屬性綁定。

 [editor]="value"

有可能,您應該將選擇器括在方括號中

import { Component, OnInit, Input } from "@angular/core";

@Component({
    selector: '[editor]', //Same as component input
    template: `<span>Foo Bar template</span>`,
})
export class TryComponent implements OnInit {
    @Input() editor: string; //Same name as selector
    @Input() color: string;
    constructor() { }

    ngOnInit() {
        console.info(this.editor);
    }
}

然后像這樣使用

<div [editor]="'FooBarValue'" color="blue"></div>

要么

<div [editor]="classProperty" color="blue"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM