1
이 예제를 따라 Angular 2 자습서에서 양식 연결을 시도합니다. 나는 로그인 페이지에 대한 코드를 적용하고있다. 구성 요소에서각도 2 - 예제 코드 정의 된 모델 속성 정의되지 않음
import { Component, OnInit } from "@angular/core";
import { LoginModel } from "../Models/login.model";
@Component({
selector: "login-form",
templateUrl: "views/login.component.html",
})
export class LoginComponent implements OnInit {
model = new LoginModel(111, "true");
submitted = false;
ngOnInit() {
this.model.id = 111;
this.model.password = "true";
}
onSubmit(): void {
this.submitted = true;
}
// remove - used for debugging
get diagnostic(): string {
return "HERE: " + JSON.stringify(this.model);
}
}
하는 OnInit 아마 필요하지 않습니다 여기에 https://angular.io/docs/ts/latest/guide/forms.html
내 모델이며, 여기에
export class LoginModel {
constructor(
public id: number,
public password: string
) {}
}
내 구성 요소입니다은, 그냥 그것을 시도 줄 거라 생각 했어요. 마지막으로 여기
내 HTML은,
<div class="container">
<h1>Login Form</h1>
{{diagnostic}}
<form *ngIf="model">
<div class="form-group col-md-6">
<label for="id">User ID:</label>
<input type="text" class="form-control" id="id" name="id"
required
[(ngModel)]="model.id"
>
</div>
<div class="form-group col-md-6">
<label for="password">Password</label>
<input type="text" class="form-control" id="password" name="password"
required
[(ngModel)]="model.password"
>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
내가으로 실행 계속 오류가 "model.id는"정의되지 않은 것입니다. 이를 확인하기 위해 * ngIf = "model"을 추가했습니다. 내 모듈을 통해 가져온 LoginComponent에 정의 된 모델이 여기에 정의되지 않은 이유가 확실하지 않습니다.
도움이 될 것입니다. 나는이 비트에 잠시 머물러있어 웹 검색에 도움이되는 것을 찾을 수 없었습니다.
확인 감사 : 당신은 내가 위에서 제공 한 코드에서 생성 된 plunker을
FormsModule
하고 여기에ReactiveFormsModule
입니다 누락 내가 누락 된NgModule
정의 빈칸을 채워처럼 보인다. ReactiveFormsModule을 추가하면 FormsModule을 가져올 때 차이가 없습니다. 내 비주얼 스튜디오에 설치되어있는 환경에 근본적으로 잘못된 것이 있을지도 모른다. 깨끗한 설치/새로운 프로젝트를 만들고 그 것이 해결되는지 확인하려고 노력할 것입니다. 감사! – user3333134기적으로 나는 오늘 아침에 내 컴퓨터를 시작하고 오래된 프로젝트에 한번 더 시도해보기로 결정했다. 솔직히 왜 시각 스튜디오가 엉망이되기 전에 실패했는지 설명 할 수 없습니다. 다시 한번 감사드립니다. – user3333134
다행이 듣고 있습니다. IntelliJ에서 최근에 IDE의 캐시를 지우고 다시 시작한 후 IntelliJ에서 강제로 다시 색인화하는 것으로 보이는 문제가있었습니다. –