I'm trying to access a DOM element outside the current directive's element.. for a resize-like behavior, using an outside element as a handle and to not change the target markup that much, especially not using transclusion like <ng-content>.
What I'm doing right now is something I think a bit unorthodox, like using the BrowserDomAdapter:
import {Directive, Input, HostListener} from 'angular2/core';
import {BrowserDomAdapter} from 'angular2/platform/browser';
@Directive
({
selector: '[resizable-handle]',
providers: [BrowserDomAdapter]
})
export class ResizableHandle
{
// get selector from actual directive selector attribute
@Input('resizable-handle') resizableSelector: string;
constructor(private _domAdapter: BrowserDomAdapter){}
@HostListener('mousedown', ['$event'])
startResize(e: MouseEvent)
{
this._domAdapter.query(this.resizableSelector); //... manipulate this native element
}
//... and so on...
}
And the template looks like this:
<a [resizable-handle]="'.target-container'"> ... </a>
<!-- ...somewhere further, on a different level, the target I don't want to touch in order to get this working... -->
<div class="target-container"> ... </div>
<ng-content>in your case?