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

问题描述 投票:0回答:2

例如,

    @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>

直到现在我的经验是,这不起作用。有没有人对如何使这项工作有任何想法?或者,如果它甚至可能?

javascript angular data-binding angular-components
2个回答
0
投票

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

 [editor]="value"

0
投票

有可能,你应该将你的选择器括在方括号中

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>
© www.soinside.com 2019 - 2024. All rights reserved.