[英]How to provide input to javascript web components from angular app
I have create JavaScript WebComponent using HTMLElement class. 我已经使用HTMLElement类创建了JavaScript WebComponent。 And build it using npx webpack. 并使用npx webpack构建它。 Now trying to use that component in Angular 7 project but when I try to provide input from angular by creating a variable it doesn't work but when I give direct string input it works. 现在尝试在Angular 7项目中使用该组件,但是当我尝试通过创建变量从angular提供输入时,它不起作用,但是当我提供直接字符串输入时,它起作用了。 Below is my web component. 以下是我的Web组件。 Basically a loader with show/hide input. 基本上是带有显示/隐藏输入的加载器。
if (typeof require !== 'undefined') {
require('../css/styles.css');
}
class Loader extends HTMLElement {
constructor() {
super();
this.initialized = false
}
static get observedAttributes() {
return ['data-input'];
}
attributeChangedCallback(name, oldValue, newValue) {
console.log(name, newValue);
if (name == 'data-input') {
if (newValue == 'show' && this.shadowRoot) {
this.shadowRoot.getElementById('my-component').className = 'show';
} else if (this.shadowRoot) {
this.shadowRoot.getElementById('my-component').className = 'hide';
}
}
if (!this.initialized) {
this.buildElement();
this.initialized = true
}
}
buildElement() {
const shadowRoot = this.attachShadow({mode: 'open'});
let style = document.createElement('style');
let cssFile = ''
if (typeof require !== 'undefined') {
cssFile = require('../css/styles.css');
}
style.textContent = cssFile;
shadowRoot.appendChild(style);
shadowRoot.appendChild(this.createElement());
}
createElement() {
const container = document.createElement("DIV");
container.id = "my-component";
container.className = "hide";
const loader = document.createElement("DIV");
loader.className = "loader";
container.appendChild(loader);
const locker = document.createElement("DIV");
locker.className = "locker";
container.appendChild(locker);
return container;
}
}
window.customElements.define('my-component', My);
This works. 这可行。
<my-component data-input="show"></my-component>
But this do not work. 但这不起作用。
<my-component data-input="{{showHideInput}}"></my-component>
Did you try: 你试过了吗:
<my-component data-input={{showHideInput}}></my-component>
? <my-component data-input={{showHideInput}}></my-component>
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.