私はAngles(4)on Railsアプリケーションで作業していて、CRUD機能を終了しようとしています。私はフロントエンドの面で「編集」を扱う方法についていません。Angular4フォームビルダダイナミック
ボタンをクリックしたときにロジックを手動でテストしましたが、フォームを事前に入力するのが苦労しています。
提案フロー
- ユーザー
getClients()
機能を実行Clients
をクリックします。 Clients
ページには、clients/:id
ページに移動するクライアントごとにID
がクリック可能です。clients/:id
はparams['id']
- 形態で
getClientById()
機能通過が
マイ・コンポーネントを実行しているeditClientById
に提出された情報
@Component({
selector: 'app-client-index',
templateUrl: './client-index.component.html',
styleUrls: ['./client-index.component.sass']
})
export class ClientIndexComponent implements OnInit {
constructor(private clientService: ClientService,
private activatedRoute: ActivatedRoute,
private fb: FormBuilder
) { }
public indexForm: FormGroup;
public submitted: boolean;
public events: any[] = [];
client = Client;
ngOnInit() {
this.activatedRoute.params.subscribe((params: Params) => {
let id = params['id'];
this.clientService.getClientById(params['id']);
this.indexForm = new FormGroup({
name: ['', <any>Validators.required],
status: [''],
logo: ['']
});
}
}
edit(model: Client, isValid: boolean) {
this.submitted = true;
console.log(model, isValid);
}
// demo(){
// console.log('pressed');
// this.clientService.editClientById({
// 'id': 4,
// 'name': 'Demo User2222',
// 'status': 'This',
// 'logo': "Test"
// })
// }
}
サービス
getClientById(id): Observable<Client> {
return this.authToken.get('clients/' + id)
.map(res => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
editClientById(client:any): Observable<Client> {
return this.authToken.patch('clients/' + client.id, client)
.map(res => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
現在ngOnInit
機能は、私は夫婦SO記事や私が見てきたスコッチのチュートリアルに基づいてしようとしているものです。これまで私が得た最も近いものは、APIに送信ボタンpatch
を送信していましたが、フィールドにあらかじめ入力していませんでした。
現在の設定では、まったく動作していません。 ngOnInitのロードが完了する前に私のgetClientById
が実行されているか、まったく機能しているかどうかは分かりません。
私はモデルにバインドする必要があると思いますが、なんとなくわかりません。
なぜフォームにデータが入力されると思いますか?リクエストが終了する(またはトリガーするためにサブスクライブする)のを待って、フォームを空にするよう明示的に設定します。 – jonrsharpe
@jonrsharpeこれは完全に公正な点であり、本質的に私が問題を抱えているところです。私が 'status:[this.client.status] 'の効果を得るために何かすると、私はそれがローディング・レートによるものと思うようになったプロパティ・エラーを取得します。 – DNorthrup
なぜローディング・レートが重要なのでしょうか? 'this.client'に何も割り当てません。 – jonrsharpe