What does the “ng-reflect-*” attribute do in Angular2/4?

前端 未结 1 862
萌比男神i
萌比男神i 2020-11-27 17:46

Here I have a complex data structure in an Angular4 application.

It is a directed multigraph parametrized with dictionaries both on nodes and on links. My angular co

相关标签:
1条回答
  • 2020-11-27 18:30

    ng-reflect-${name} attributes are added for debugging purposes and show the input bindings that a component/directive has declared in its class. So if your component is declared like this:

    class AComponent {
      @Input() data;
      @Input() model;
    }
    

    the resulting html will be rendered like this:

    <a-component ng-reflect-data="..." ng-reflect-model="...">
       ...
    <a-component>
    

    They exist only when debugging mode is used - default mode for Angular. To disable them, use

    import {enableProdMode} from '@angular/core';
    
    enableProdMode();
    

    inside main.ts file. These attributes are added by this function here:

    function debugCheckAndUpdateNode(...): void {
      const changed = (<any>checkAndUpdateNode)(view, nodeDef, argStyle, ...givenValues);
      if (changed) {
        const values = argStyle === ArgumentType.Dynamic ? givenValues[0] : givenValues;
        if (nodeDef.flags & NodeFlags.TypeDirective) {
          const bindingValues: {[key: string]: string} = {};
          for (let i = 0; i < nodeDef.bindings.length; i++) {
            const binding = nodeDef.bindings[i];
            const value = values[i];
            if (binding.flags & BindingFlags.TypeProperty) {
              bindingValues[normalizeDebugBindingName(binding.nonMinifiedName !)] =
                  normalizeDebugBindingValue(value); <------------------
            }
          }
    
        ...
    
        for (let attr in bindingValues) {
          const value = bindingValues[attr];
          if (value != null) {
            view.renderer.setAttribute(el, attr, value); <-----------------
    
    0 讨论(0)
提交回复
热议问题