2016-06-24 2 views
0

공유 모델 변수의 형식이 하위 구성 요소와 호환되지 않습니다. 응용 프로그램 모델을 엉망으로 만들지 않고 변환을 어떻게 처리해야합니까?호환되지 않는 형식의 공유 모델 변수

import {Calendar} from 'primeng/primeng'; 

@Component({ 
    selector: 'foo', 
    directives: [Calendar], 
    template: ` 
    ... 
    <p-calendar [(ngModel)]="model"></p-calendar> 
    ... 
`}) 
class FooComponent { 
    // input format: "1970-01-01T00:00:00Z" 
    // model is shared within the app 
    // should stay in this format 
    // Calendar doesn't support this format 
    @Input() model; 


} 

답변

0

내가 정말로 좋아하지 않는 방식으로 해결되었지만 더 좋은 해결책은 환영합니다. 감사합니다. .

@Pipe({ 
    name: 'iso2date' 
}) 
export class Iso2Date { 
    transform(value: any, args: Array<any>): string { 
     return moment(value).format("YYYY-MM-DD"); 
    } 
} 

@Component({ 
    selector: 'foo', 
    directives: [Calendar], 
    pipes: [Iso2Date], 
    template: ` 
    ... 
    <p-calendar dateFormat="yy-mm-dd" 
     [ngModel]="model | iso2date" 
     (ngModelChange)="model=date2iso($event)"></p-calendar> 
    ... 
`}) 
class FooComponent { 
    @Input() model; 

    date2iso(value) { 
     var m = moment(value, "YYYY-MM-DD"); 
     return m.toISOString(); 
    } 

} 

p.s : 파이프는 다음 클리너했을 것이다, 그렇지 않으면 행동에서 작동하지 않습니다 그러나

@Pipe({ 
    name: 'date2iso' 
}) 
export class Date2Iso { 
    transform(value: any, args: Array<any>): string { 
     return moment(value,"YYYY-MM-DD").toISOString(); 
    } 
} 

... 
(ngModelChange)="model=(event | date2iso)"></p-calendar> 
... 

나는 다음과 같은 오류 얻을 :

Parser Error: Cannot have a pipe in an action expression at column 20 in [model=($event | date2iso)] in [email protected]:28 ("="yy-mm-dd" 
          [ngModel]="model | iso2date" 
          [ERROR ->](ngModelChange)="model=($event | date2iso)" 
관련 문제