• [Angular] Communicate with Angular Elements using Inputs and Events


    In a real world scenario we obviously need to be able to communicate with an Angular Element embedded into our static HTML site. In this lesson we will learn how we can pass data into a Custom Element and how we can register to an Angular Element’s output in the form of custom events.

    import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
    
    @Component({
      // selector: 'do-greet',
      template: `
        <div>Hi, {{ name }}!</div>
        <button (click)="doGreet()">Greet</button>
      `,
      styles: []
    })
    export class GreeterComponent implements OnInit {
      @Input() name;
      @Output() greet = new EventEmitter();
    
      constructor() {}
    
      doGreet() {
        this.greet.emit(`Hi, ${this.name}`);
      }
    
      ngOnInit() {}
    }

    Listening for the events:

    <html>
      <body>
        <do-greet name="Juri"></do-greet>
    
        <script src="./polyfills.js"></script>
        <script src="./ngelements.js"></script>
        <script>
          const el = document.querySelector('do-greet');
          el.addEventListener('greet', ev => {
            alert(ev.detail); // get the event output from 'detial'
          });
        </script>
      </body>
    </html>
  • 相关阅读:
    POJ2524+并查集
    POJ3697+BFS+hash存边
    POJ1151+线段树+扫描线
    POJ2528+线段树
    ubuntu 和 win7 远程登陆 + vnc登陆
    POJ3690+位运算
    POJ3283+字典树
    POJ3282+模拟
    POJ2349+prim
    2016.6.13
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10559764.html
Copyright © 2020-2023  润新知