-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(common): ngLet directive #25472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e39f321
feat(common): Implemented ng-let directive.
ibarsi 284e397
style(common): Fix formatting in previous commit.
ibarsi 0344681
test(common): Added tests for new ngLet directive.
ibarsi b9f2780
refactor(common): Create embedded view only on init. Rename input
ibarsi 0269a5d
style(common): Fix formatting in previous commit.
ibarsi 520f56f
test(common): Update tests to better reflect intended functionality,
ibarsi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
/** | ||
* @license | ||
* Copyright 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 https://angular.io/license | ||
*/ | ||
|
||
import {Directive, Input, OnInit, TemplateRef, ViewContainerRef} from '@angular/core'; | ||
|
||
|
||
|
||
/** | ||
* Exposes a stored expression value to its child view. | ||
* | ||
* `ngLet` evaluates the expression and, if stored in a variable, provides the result to its child | ||
* view. | ||
* | ||
* | ||
* @usageNotes | ||
* | ||
* ### Storing falsy result in a variable | ||
* | ||
* `ngIf` supports the common use case of storing a value in a variable and exposing it to its child | ||
* view. | ||
* | ||
* {@example common/ngIf/ts/module.ts region='NgIfAs'} | ||
* | ||
* This works well for most cases. However, if the value stored is falsy (ie. `false`, `0`, `''`, | ||
* etc) | ||
* the child view will not be rendered. This can be problematic if the stored value is required to | ||
* be displayed, | ||
* or provided as an argument to an output or DOM event. | ||
* | ||
* `ngLet` works around this issue by always rendering the child view, regardless of the result of | ||
* its expression. | ||
* | ||
* ### Semantic meaning | ||
* | ||
* Even in the cases where `ngIf` works, some semantic meaning is lost. Reading the code doesn't | ||
* communicate whether | ||
* the auther intended contitional rendering or simply needed to expose the result of the expression | ||
* to its child view. | ||
* | ||
* `ngLet` explicitly communicates semantic meaning, illustrating that the intent of the author's | ||
* code was to expose the | ||
* result of the expression, not conditional rendering. | ||
* | ||
* ### Syntax | ||
* | ||
* Simple form: | ||
* - `<div *ngLet="condition as value">{{value}}</div>` | ||
* - `<ng-template [ngLet]="condition as value"><div>{{value}}</div></ng-template>` | ||
* | ||
* | ||
*/ | ||
@Directive({selector: '[ngLet]'}) | ||
export class NgLet implements OnInit { | ||
private _context: NgLetContext = new NgLetContext(); | ||
|
||
constructor( | ||
private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef<NgLetContext>, ) { | ||
} | ||
|
||
@Input() | ||
set ngLet(value: any) { this._context.$implicit = this._context.ngLet = value; } | ||
|
||
ngOnInit() { this._viewContainer.createEmbeddedView(this._templateRef, this._context); } | ||
} | ||
|
||
export class NgLetContext { | ||
public $implicit: any = null; | ||
public ngLet: any = null; | ||
} |
This file contains hidden or 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,74 @@ | ||
/** | ||
* @license | ||
* Copyright 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 https://angular.io/license | ||
*/ | ||
|
||
import {CommonModule} from '@angular/common'; | ||
import {Component, EventEmitter} from '@angular/core'; | ||
import {ComponentFixture, TestBed, async} from '@angular/core/testing'; | ||
import {By} from '@angular/platform-browser/src/dom/debug/by'; | ||
import {expect} from '@angular/platform-browser/testing/src/matchers'; | ||
|
||
{ | ||
describe('ngLet directive', () => { | ||
let fixture: ComponentFixture<any>; | ||
|
||
function getComponent(): TestComponent { return fixture.componentInstance; } | ||
|
||
afterEach(() => { fixture = null !; }); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestComponent], | ||
imports: [CommonModule], | ||
}); | ||
}); | ||
|
||
it('should support binding to observable using as', async(() => { | ||
const template = '<span *ngLet="observableStringValue | async as val">{{val}}</span>'; | ||
fixture = createTestComponent(template); | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('span')).length).toEqual(1); | ||
expect(fixture.nativeElement).toHaveText(''); | ||
|
||
getComponent().observableStringValue.emit('some string'); | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement).toHaveText('some string'); | ||
})); | ||
|
||
it('should support binding to variable using as', async(() => { | ||
const template = '<span *ngLet="stringValue as val">{{val}}</span>'; | ||
fixture = createTestComponent(template); | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('span')).length).toEqual(1); | ||
expect(fixture.nativeElement).toHaveText('some string'); | ||
})); | ||
|
||
it('should support binding to variable using let', async(() => { | ||
const template = '<span *ngLet="stringValue; let v">{{v}}</span>'; | ||
|
||
fixture = createTestComponent(template); | ||
|
||
fixture.detectChanges(); | ||
expect(fixture.nativeElement).toHaveText('some string'); | ||
|
||
getComponent().stringValue = 'some other string'; | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement).toHaveText('some other string'); | ||
})); | ||
}); | ||
} | ||
|
||
@Component({selector: 'test-cmp', template: ''}) | ||
class TestComponent { | ||
stringValue: string = 'some string'; | ||
observableStringValue: EventEmitter<any> = new EventEmitter(); | ||
} | ||
|
||
function createTestComponent(template: string): ComponentFixture<TestComponent> { | ||
return TestBed.overrideComponent(TestComponent, {set: {template: template}}) | ||
.createComponent(TestComponent); | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont use
EventEmitter
for the purpose of anObservable
instance. Use aSubject
or similar.