简体   繁体   English

如何从angular应用程序向javascript web组件提供输入

[英]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>吗?

https://alligator.io/angular/using-custom-elements/添加集合并使属性工作。

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

相关问题 Angular Web 组件 - @Input 没有收到值 - Angular Web Components - @Input not receiving the value 如何从Angular 2中的多个组件将数据传递给单个@Input? - How to pass data to single @Input from multiple components in Angular 2? 在Angular应用中使用JavaScript组件/插件 - Using JavaScript components/plugins in an Angular app 模块联合:如何在 Shell 应用程序中为 MFE 应用程序 Angular 元素 web 组件添加EventListener - Module-Federation: How to addEventListener in Shell app for MFEs App Angular element web components 如何使用javascript将文件输入字段复制到我的根Web应用中? - How to copy file input field into my root web app with javascript? Web 组件:如何监听表单输入变化 - Web Components: How to listen to form input changes 如何在影子 dom/web 组件中执行 javascript? - How to execute javascript in shadow dom/web components? 如何使用Web组件缓解JavaScript库膨胀? - How is JavaScript library bloat mitigated with Web Components? 如何使用快速服务器为 web 组件应用程序提供服务 - How to serve web components app with an express server @Input()无法在Angular 2 App中的父子组件之间传递 - @Input() Not Passing As Expected Between Parent-Child Components in Angular 2 App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM