简体   繁体   中英

what does [] mean in angular 2 selector

I'm studying the primeNG source code and here is one selector confuses me. The original code is as follows:

@Component({
    selector: '[pTreeRow]',
    template: `
        <div [class]="node.styleClass" [ngClass]="{'ui-treetable-row': true, 'ui-state-highlight':isSelected(),'ui-treetable-row-selectable':treeTable.selectionMode && node.selectable !== false}">
            <td *ngFor="let col of treeTable.columns; let i=index" [ngStyle]="col.bodyStyle||col.style" [class]="col.bodyStyleClass||col.styleClass" (click)="onRowClick($event)" (dblclick)="rowDblClick($event)" (touchend)="onRowTouchEnd()" (contextmenu)="onRowRightClick($event)">
                <a href="#" *ngIf="i == treeTable.toggleColumnIndex" class="ui-treetable-toggler fa fa-fw ui-clickable" [ngClass]="node.expanded ? treeTable.expandedIcon : treeTable.collapsedIcon"
                    [ngStyle]="{'margin-left':level*16 + 'px','visibility': isLeaf() ? 'hidden' : 'visible'}"
                    (click)="toggle($event)"
                    [title]="node.expanded ? labelCollapse : labelExpand">
                </a>
                <div class="ui-chkbox ui-treetable-checkbox" *ngIf="treeTable.selectionMode == 'checkbox' && i==0"><div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default">
                    <span class="ui-chkbox-icon ui-clickable fa" 
                        [ngClass]="{'fa-check':isSelected(),'fa-minus':node.partialSelected}"></span></div></div
                ><span *ngIf="!col.template">{{resolveFieldData(node.data,col.field)}}</span>
                <ng-container *ngTemplateOutlet="col.template; context: {$implicit: col, rowData: node}"></ng-container>
            </td>
        </div>
        <div *ngIf="node.children && node.expanded" class="ui-treetable-row" style="display:table-row">
            <td [attr.colspan]="treeTable.columns.length" class="ui-treetable-child-table-container">
                <table [class]="treeTable.tableStyleClass" [ngStyle]="treeTable.tableStyle">
                    <tbody pTreeRow *ngFor="let childNode of node.children" [node]="childNode" [level]="level+1" [labelExpand]="labelExpand" [labelCollapse]="labelCollapse" [parentNode]="node"></tbody>
                </table>
            </td>
        </div>
    `
})

My question is about the meaning of [] in the selector, since it is pTreeRow instead of [pTreeRow] in the following code:

<tbody pTreeRow *ngFor="let childNode of node.children" [node]="childNode" [level]="level+1" [labelExpand]="labelExpand" [labelCollapse]="labelCollapse" [parentNode]="node"></tbody>

So what is the meaning of [] ?

I tried to delete [] and only remain selector: 'pTreeRow', and the error from the chrome console shows as follows:

Can't bind to 'node' since it isn't a known property of 'tbody'. ("             <tbody pTreeRow *ngFor="let node of value" class="ui-treetable-data ui-widget-content" [ERROR ->][node]="node" [level]="0" [labelExpand]="labelExpand" [labelCollapse]="labelCollapse"></tbody>
      "): ng:///TreeTableModule/TreeTable.html@28:107
Can't bind to 'level' since it isn't a known property of 'tbody'. ("tbody pTreeRow *ngFor="let node of value" class="ui-treetable-data ui-widget-content" [node]="node" [ERROR ->][level]="0" [labelExpand]="labelExpand" [labelCollapse]="labelCollapse"></tbody>
                </ta"): ng:///TreeTableModule/TreeTable.html@28:121
Can't bind to 'labelExpand' since it isn't a known property of 'tbody'. ("ow *ngFor="let node of value" class="ui-treetable-data ui-widget-content" [node]="node" [level]="0" [ERROR ->][labelExpand]="labelExpand" [labelCollapse]="labelCollapse"></tbody>
                </table>
       "): ng:///TreeTableModule/TreeTable.html@28:133
Can't bind to 'labelCollapse' since it isn't a known property of 'tbody'. ("" class="ui-treetable-data ui-widget-content" [node]="node" [level]="0" [labelExpand]="labelExpand" [ERROR ->][labelCollapse]="labelCollapse"></tbody>
                </table>
            </div>

Can someone tell me the meaning of [] ? Thanks!

selector: 'pTreeRow' means select elements having name <pTreeRow></pTreeRow>

selector: '[pTreeRow]' means select elements having attribute <whatever pTreeRow></whatever>

The [] means that it is selected an attribute on the element. So if you had a div like this:

<div someAttribute></div>

And you had a selector for [someAttribute] would select this div and use it for the component. This is especially common when working with custom SVG components.

Example:

in some html file...

<svg:text my-svg-text></svg:text>

your component code for 'my-svg-text-component'

@Component({
  selector: `[my-svg-text]`,
  template: `...`
})
export class MySvgTextComponent { }

Brackets [selector-name] selector is an attribute selector. You're using it like this:

<div [selector-name]></div>

Without brackets, you have element selector:

<selector-name></selector-name>

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