Closed
Description
By using a directive rather than a component, the drop area can be easily added on to templating which is controlled by another component.
An example of would be in the MatTable
/CdkTable
:
Currently you must do the following for each column:
<ng-container cdkColumnDef="position">
<th cdk-header-cell *cdkHeaderCellDef>
<cdk-drop [connectedTo]="drops">
<span cdkDrag>No.</span>
</cdk-drop>
</th>
<td cdk-cell *cdkCellDef="let element"> {{element.position}} </td>
</ng-container>
coupled with something in your components class to determine all of the drop zones.
@ViewChildren(CdkDrop) dropChildren: QueryList<CdkDrop>;
drops: CdkDrop[] = [];
ngAfterInit() {
this.dropChildren.changes.subscribe(() => {
this.drops = this.dropsChildren.toArray();
});
// Simple hack for quick example
Promise.resolve().then(() => {
this.drops = this.dropsChildren.toArray();
});
}
By moving to using directives we can have the following:
<ng-container cdkColumnDef="position">
<th cdk-header-cell *cdkHeaderCellDef cdkDrag>No.</th>
<td cdk-cell *cdkCellDef="let element"> {{element.position}} </td>
</ng-container>
<tr cdk-header-row *cdkHeaderRowDef="displayedColumns" cdkDrop></tr>