简体   繁体   中英

why Angular component selector can be included in html?

I'm new to Angular and html/html5, sorry if my questions sound dumb. Below is a screenshot:

在此处输入图像描述

My questions are:

Q1- paproductform is ng component selector's name, which is not a valid html tag, how come it can still be included in html?How client browsers interpret those invalid tags?

Q2- You can see that there are a lots of ng related attributes like _nghost-c0 , isn't that custom attributes in html has to be data-xxx(data-_nghost-c0) to be valid?

According to Angular Documentation:

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.

Custom elements are a Web Platform feature currently supported by Chrome, Firefox, Opera, and Safari, and available in other browsers through polyfills (see Browser Support). A custom element extends HTML by allowing you to define a tag whose content is created and controlled by JavaScript code. The browser maintains a CustomElementRegistry of defined custom elements, which maps an instantiable JavaScript class to an HTML tag.


A1 - Creating custom HTML Elements using CustomElementRegistry .

use the CustomElementRegistry.define() method to define the custom element after creating its class:

class WordCount extends HTMLParagraphElement {
  constructor() {

    super(); 

    var wcParent = this.parentNode;   // count words in element's parent element

    function countWords(node){
      var text = node.innerText || node.textContent
      return text.split(/\s+/g).length;
    }

    var count = 'Words: ' + countWords(wcParent);

    var shadow = this.attachShadow({mode: 'open'});  // Create a shadow root

    // Create text node and add word count to it
    var text = document.createElement('span');
    text.textContent = count;

    shadow.appendChild(text);  // Append it to the shadow root

    setInterval(function() {  // Update count when element content changes
      var count = 'Words: ' + countWords(wcParent);
      text.textContent = count;
    }, 200)

  }
}

// Define the new element
customElements.define('word-count', WordCount, { extends: 'p' });



A2 - Creating custom attribue using Element.setAttribute() .

In the following example, setAttribute() is used to set attributes on a <button> .

HTML

<button>Hello World</button>

JavaScript

var b = document.querySelector("button"); 

b.setAttribute("name", "helloButton");
b.setAttribute("disabled", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM