2017-10-30 1 views
0

새 데이터를 편집하고 삽입하는 작업에 사용할 양식 구성 요소를 만들었으므로 구성 요소의 개체를 나타내는 변수를 만들었고 [(ngModel) = "schoolClass.name"그러나 다음과 같은 오류가 콘솔에 나타난다 : "schoolClass .name을"지만을 사용할 때 나타나는ngModel을 통해 변수에 액세스 할 수 없습니다. - 각도 4

ERROR TypeError: Cannot read property 'name' of null 
    at Object.eval [as updateDirectives] (SchoolClassFormComponent.html:5) 
    at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13067) 
    at checkAndUpdateView (core.es5.js:12251) 
    at callViewAction (core.es5.js:12599) 
    at execComponentViewsAction (core.es5.js:12531) 
    at checkAndUpdateView (core.es5.js:12257) 
    at callViewAction (core.es5.js:12599) 
    at execEmbeddedViewsAction (core.es5.js:12557) 
    at checkAndUpdateView (core.es5.js:12252) 
    at callViewAction (core.es5.js:12599) 
    at execComponentViewsAction (core.es5.js:12531) 
    at checkAndUpdateView (core.es5.js:12257) 
    at callViewAction (core.es5.js:12599) 
    at execEmbeddedViewsAction (core.es5.js:12557 

은 이미 다음 연산자 [(ngModel) =를 사용한 이 오류 :

ZoneAwareError {__zone_symbol__error: Error: Uncaught (in promise): Error: Template parse errors: Parser Error: The '?.' operator cannot b…, …} 
message : "Uncaught (in promise): Error: Template parse errors:↵Parser Error: The '?.' operator cannot be used in the assignment at column 19 in [schoolClass?.name=$event] in ng:///SchoolClassModule/[email protected]:37 ("↵ <div class="row">↵  <div class="input-field col s12">↵  <input type="text" id="name" [ERROR ->][(ngModel)]="schoolClass?.name" name="name" class="validate">↵ .... 

이들은 내 프로젝트의 요소입니다.

학교 클래스 form.component.ts :

export class SchoolClassFormComponent implements OnInit, OnDestroy { 
    schoolClass: SchoolClass; 
    ..... 
} 

학교 클래스 form.component.html :

<input type="text" id="name" [(ngModel)]="schoolClass.name" name="name" class="validate"> 

학교 class.ts :

export class SchoolClass { 

constructor( 
    private _name : string 
) { } 

..... 

get name() : string { 
    return this._name; 
} 

set name(name : string) { 
    this._name = name; 
} 
..... 
} 

도와주세요!

+0

https://stackoverflow.com/questions/45144230/conditional-ngmodel-in -angular2 – yurzui

+0

https://github.com/angular/angular/issues/7697 – yurzui

답변

0

schoolClass의 유형 만 설정합니다. schoolClass을 초기화해야합니다.

export class SchoolClassFormComponent implements OnInit, OnDestroy { 
    schoolClass: SchoolClass = new SchoolClass('Name'); // What name you want 
    ..... 
} 
+0

이 구성 요소를 사용하여 새 데이터를 편집하고 저장하기 때문에 null로 시작했습니다. –

+0

기본값을 사용하여 초기화하거나 변수를 사용하려는 태그에'* ngIf = "schoolClass"'를 추가하십시오. –

+0

좋아요, 여기서 테스트 할게요! –

0

당신은 당신은 단지 현재 아무것도를 가리키는 학교 클래스의 참조가 먼저 SchoolClass 클래스를 인스턴스화 할 필요가

관련 문제