Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): add attribute directive example
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
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
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
28 changes: 28 additions & 0 deletions
28
playground/app/components/attribute-directive/attribute-directive-example.component.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Component } from 'ng-metadata/core'; | ||
|
||
import { HighlightDirective } from './highlight.directive'; | ||
|
||
@Component({ | ||
selector: 'my-attribute-directive-example', | ||
template: ` | ||
<h1>My First Attribute Directive</h1> | ||
<h4>Pick a highlight color</h4> | ||
<div> | ||
<input type="radio" name="colors" ng-click="$ctrl.color='lightgreen'">Green | ||
<input type="radio" name="colors" ng-click="$ctrl.color='yellow'">Yellow | ||
<input type="radio" name="colors" ng-click="$ctrl.color='cyan'">Cyan | ||
</div> | ||
<p my-highlight [color]="$ctrl.color" (on-color-click)="$ctrl.colorClicked($event)">Highlight me!</p> | ||
<p my-highlight [color]="$ctrl.color" [default-color]="'violet'">Highlight me too!</p> | ||
`, | ||
directives: [HighlightDirective] | ||
}) | ||
export class AttributeDirectiveExampleComponent { | ||
color: string; | ||
|
||
colorClicked(sentence: string){ | ||
console.log(`color clicked! ${sentence}`); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
playground/app/components/attribute-directive/highlight.directive.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Directive, HostListener, Input, Inject, Output, EventEmitter } from 'ng-metadata/core'; | ||
|
||
@Directive({ | ||
selector: '[myHighlight]' | ||
}) | ||
export class HighlightDirective { | ||
private _defaultColor = 'red'; | ||
private el: HTMLElement; | ||
|
||
constructor( | ||
@Inject('$element') $element: ng.IAugmentedJQuery | ||
) { | ||
this.el = $element[0]; | ||
} | ||
|
||
@Input() set defaultColor(colorName: string){ | ||
this._defaultColor = colorName || this._defaultColor; | ||
} | ||
|
||
@Input('color') highlightColor: string; | ||
|
||
@Output() onColorClick = new EventEmitter<string>(); | ||
|
||
@HostListener('mouseenter') | ||
onMouseEnter() { | ||
this.highlight(this.highlightColor || this._defaultColor); | ||
} | ||
@HostListener('mouseleave') | ||
onMouseLeave() { | ||
this.highlight(null); | ||
} | ||
@HostListener('click') | ||
onClick() { | ||
this.onColorClick.emit('yes it was ME!'); | ||
} | ||
|
||
private highlight(color: string) { | ||
this.el.style.backgroundColor = color; | ||
} | ||
} | ||
/* | ||
@Input() myHighlight: string; | ||
*/ | ||
|
||
|
||
/* | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
Use of this source code is governed by an MIT-style license that | ||
can be found in the LICENSE file at http://angular.io/license | ||
*/ |