Skip to content

Instantly share code, notes, and snippets.

@jeffbcross
Created May 15, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbcross/4349fc70b5a426d0ed75 to your computer and use it in GitHub Desktop.
Save jeffbcross/4349fc70b5a426d0ed75 to your computer and use it in GitHub Desktop.
local-assigner
//@vsavkin did most of the work
//Angular 2 directive to allow setting a local variable from within a template.
//Usage example with an "auth" observable, whose unwrapped value gets set to "unwrapped":
/*
<span *assign-local="#unwrapped to auth | async">
<button (click)="login()" *ng-if="!unwrapped">Login</button>
</span>
*/
import {
Directive,
ViewContainerRef,
ProtoViewRef
} from "angular2/angular2";
@Directive({selector: '[assign-local]', properties: {'localVariable': 'assignLocalTo'}})
class LocalVariable {
viewContainer: ViewContainerRef;
protoViewRef: ProtoViewRef;
view: any;
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef) {
this.viewContainer = viewContainer;
this.protoViewRef = protoViewRef;
}
set localVariable(exp) {
if (!this.viewContainer.length) {
this.view = this.viewContainer.create(this.protoViewRef);
}
this.view.setLocal("$implicit", exp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment