1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| export class StockFormComponent implements OnInit { formModel:FormGroup; stockId:number; stock:Stock; categories = ["IT","互联网","金融"]; constructor( private activatedRoute: ActivatedRoute, private stockService:StockService, private router:Router ) { }
ngOnInit() { this.stockId = this.activatedRoute.snapshot.params['id']; this.stock = this.stockService.getStock(this.stockId);
let fb = new FormBuilder; this.formModel = fb.group({ name:[this.stock.name, [Validators.required, Validators.minLength(3)]], price:[this.stock.price, [Validators.required]], desc:[this.stock.desc], categories:fb.array([ new FormControl(this.stock.categories.indexOf(this.categories[0])!== -1), new FormControl(this.stock.categories.indexOf(this.categories[1])!== -1), new FormControl(this.stock.categories.indexOf(this.categories[2])!== -1) ]) }); }
cancel(){ this.router.navigateByUrl('/stock'); }
save(){ const chineseCategories = []; for(let i = 0; i< 3;i++){ if(this.formModel.value.categories[i]){ chineseCategories[i] = this.categories[i]; } } this.formModel.value.categories = chineseCategories; this.formModel.value.rating = this.stock.rating; console.log(this.formModel.value); } }
|