Javascript objects and reference variables

Опубликовано: 01 Май 2018
на канале: kudvenkat
18,482
140

In this video we will discuss
1. Javascript objects and reference variables
2. Along the way, why submitted data becomes NULL when the angular form is reset and
3. More importantly we will also discuss the fix for it

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
   / @aarvikitchen5572  

Text version of the video
http://csharp-video-tutorials.blogspo...

Slides
http://csharp-video-tutorials.blogspo...

Angular CRUD Tutorial
   • Angular CRUD tutorial  

Angular CRUD Tutorial Text Articles & Slides
http://csharp-video-tutorials.blogspo...

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenka...

All Dot Net and SQL Server Tutorials in Arabic
   / kudvenkatarabic  

This is continuation to Part 45, please watch Part 45 from Angular CRUD tutorial before proceeding.

The following is the reason the submitted data becomes NULL when the angular form is reset
The "Create Employee Form" is bound to the Employee object.
This same employee object is passed to the save method of the Employee Service.
this._employeeService.save(this.employee);
When the form is reset, the employee object which is bound to the form is also reset. This happens because of the Angular 2 way data-binding.
As the employee object that is bound to the form, and the employee object that is passed to the save() method are the same, when the form is reset, the bound employee object is reset and as a result, the employee object passed to the save() method is also reset.

To solve this create a new employee object and copy over all the values of the employee object, and then pass the newly created employee object to the save() method.

Notice in the example below, we are using Object.assign() method to copy the employee object property values into a new employee object

saveEmployee(): void {
const newEmployee: Employee = Object.assign({}, this.employee);
this._employeeService.save(newEmployee);
this.createEmployeeForm.reset();
this._router.navigate(['list']);
}

Regarding reference variables the key point to keep in mind is, object reference variables are pointers to objects, they are not objects themselves.


На этой странице сайта вы можете посмотреть видео онлайн Javascript objects and reference variables длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь kudvenkat 01 Май 2018, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 18,482 раз и оно понравилось 140 зрителям. Приятного просмотра!