fix(common): support numeric value as discrete cases for NgPlural (#13876)

PR Close #13876
This commit is contained in:
Marc Laval 2017-01-11 16:32:34 +01:00 committed by Miško Hevery
parent c2aa981dd6
commit f364557629
2 changed files with 19 additions and 2 deletions

View File

@ -103,6 +103,7 @@ export class NgPluralCase {
constructor( constructor(
@Attribute('ngPluralCase') public value: string, template: TemplateRef<Object>, @Attribute('ngPluralCase') public value: string, template: TemplateRef<Object>,
viewContainer: ViewContainerRef, @Host() ngPlural: NgPlural) { viewContainer: ViewContainerRef, @Host() ngPlural: NgPlural) {
ngPlural.addCase(value, new SwitchView(viewContainer, template)); const isANumber: boolean = !isNaN(Number(value));
ngPlural.addCase(isANumber ? `=${value}` : value, new SwitchView(viewContainer, template));
} }
} }

View File

@ -12,7 +12,7 @@ import {ComponentFixture, TestBed, async} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers'; import {expect} from '@angular/platform-browser/testing/matchers';
export function main() { export function main() {
describe('switch', () => { describe('ngPlural', () => {
let fixture: ComponentFixture<any>; let fixture: ComponentFixture<any>;
function getComponent(): TestComponent { return fixture.componentInstance; } function getComponent(): TestComponent { return fixture.componentInstance; }
@ -47,6 +47,22 @@ export function main() {
detectChangesAndExpectText('you have one message.'); detectChangesAndExpectText('you have one message.');
})); }));
it('should display the template according to the exact numeric value', async(() => {
const template = '<div>' +
'<ul [ngPlural]="switchValue">' +
'<template ngPluralCase="0"><li>you have no messages.</li></template>' +
'<template ngPluralCase="1"><li>you have one message.</li></template>' +
'</ul></div>';
fixture = createTestComponent(template);
getComponent().switchValue = 0;
detectChangesAndExpectText('you have no messages.');
getComponent().switchValue = 1;
detectChangesAndExpectText('you have one message.');
}));
// https://github.com/angular/angular/issues/9868 // https://github.com/angular/angular/issues/9868
// https://github.com/angular/angular/issues/9882 // https://github.com/angular/angular/issues/9882
it('should not throw when ngPluralCase contains expressions', async(() => { it('should not throw when ngPluralCase contains expressions', async(() => {