我可以使用 angular 组件选择器作为该组件的属性吗?
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>
例如,
@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>