繁体   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