简体   繁体   English

我可以使用角度组件选择器作为该组件的属性吗?

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

For example, 例如,

    @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> HTML: <div editor="value" color="blue"></div>

My experience until now is that this doesn't work. 到目前为止,我的经验是这行不通。 Does anyone have any ideas on how to make this work? 有人对如何进行这项工作有任何想法吗? Or if it's even possible? 还是有可能?

您应该在div标签上使用属性绑定。

 [editor]="value"

it is possible, you should enclose your selector in square braces 有可能,您应该将选择器括在方括号中

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);
    }
}

then use like this 然后像这样使用

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

or 要么

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

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

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