Я добавляю новый рецепт. После этого я хочу перейти к новому добавленному рецепту.
Как передать идентификатор созданного рецепта?
Я пробовал: this.router.navigate (['/ recipe / edit /' + id]);
но меня это не касается. В ответ я получил: http://localhost: 5000 / API / рецепты /% 7Bid% 7D 400 (неверный запрос)
this.router.navigate(['/recipe/edit/HERE MUST BE ID CREATED RECIPE']);
Мой add-recipe.component.ts
@Component({
selector: 'app-add-recipe',
templateUrl: './add-recipe.component.html',
styleUrls: ['./add-recipe.component.css']
})
export class AddRecipeComponent implements OnInit {
categories: Category[];
user: User;
recipe: Recipe;
addRecipeForm: FormGroup;
constructor(private fb: FormBuilder,
private alertifyService: AlertifyService,
private router: Router,
private recipeService: RecipeService, private authService: AuthService) { }
ngOnInit() {
this.createRecipeForm();
this.getCategories();
}
createRecipeForm() {
this.addRecipeForm = this.fb.group({
name: ['', Validators.required],
ingredients: ['', Validators.required],
preparationTime: ['', Validators.required],
description: ['', Validators.required],
categoryId: ['', Validators.required]
});
}
createRecipe(id: number) {
if (this.addRecipeForm.valid) {
this.recipe = Object.assign({}, this.addRecipeForm.value);
this.recipeService.addNewRecipe(this.authService.currentUser.id, this.recipe).subscribe(() => {
this.alertifyService.success('New recipe has been added!');
}, error => {
this.alertifyService.error(error);
}, () => {
this.router.navigate(['/recipe/edit/']);
});
}
}
cancel() {
this.router.navigate(['members']);
this.alertifyService.warning('Cancelled');
}