Created
May 15, 2015 16:36
-
-
Save jeffbcross/4349fc70b5a426d0ed75 to your computer and use it in GitHub Desktop.
local-assigner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@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