forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): new pipe to support object for ngFor
Add keys_pipe which uses Object.keys in order to transform an object to array of its keys. Fix angular#2246
- Loading branch information
1 parent
a45769a
commit 9a74a1b
Showing
5 changed files
with
51 additions
and
0 deletions.
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
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,18 @@ | ||
import {Pipe, PipeTransform} from '@angular/core'; | ||
|
||
/** | ||
* Transforms a map/object to array of keys/properties | ||
* using `Object.keys` | ||
* | ||
* ### Usage | ||
* | ||
* <div *ngFor="let key of myObject | keys"> | ||
* key is {{key}} and value is {{myObject[key]}} | ||
* </div> | ||
* | ||
* @experimental | ||
*/ | ||
@Pipe({name: 'keys', pure: false}) | ||
export class KeysPipe implements PipeTransform { | ||
transform(value: Object): Array<string> { return Object.keys(value); } | ||
} |
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,25 @@ | ||
import {KeysPipe} from '@angular/common'; | ||
import {afterEach, beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing/testing_internal'; | ||
|
||
export function main() { | ||
describe('KeysPipe', () => { | ||
var object: Object; | ||
var keys: Array<string>; | ||
var pipe: KeysPipe; | ||
|
||
beforeEach(() => { | ||
object = { key1: 'value1', key2: 'value2' }; | ||
keys = ['key1', 'key2']; | ||
pipe = new KeysPipe(); | ||
}); | ||
|
||
describe('transform', () => { | ||
it('should return keys', () => { | ||
var val = pipe.transform(object); | ||
expect(val).toEqual(keys); | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
} |
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