Reputation: 2043
I saw a piece of code in Angular and i am unable to understand it.
remove_stock({group, index}: {group: FormGroup, index: number}) {
const control = this.form.get('stock') as FormArray;
control.removeAt(index);
}
I understand there is a method called remove_stock and since it uses an parenthesis it takes an argument.
But I am not sure about this argument . I know in typescript that anything enclosed in braces is an object and whatever is preceding a colon is the name of the type.
But what i don't get is this
{group, index}: {group: FormGroup, index: number}
Can someone please explain
I have taken this from a screenshot of the course from ultimate Angular and i am stumped. Here is the link to that image
https://i.vimeocdn.com/video/680140010.webp?mw=1500&mh=844&q=70
Thanks
Upvotes: 0
Views: 76
Reputation: 527
First, this syntax have nothing to do with typescript, it's a es6 syntax.
It's called Destructuring assignment, Take a look at this link Destructuring assignment
Take a look at this code:
const user = {
id: '1',
name: 'user'
};
const {id, name} = user;
These lines of code identical as this
const user = {
id: '1',
name: 'user'
};
const id = user.id;
const name = user.name;
And your function above, that will be like this
remove_stock(input: {group: FormGroup, index: number}) {
const index = input.index;
const group = input.group;
const control = this.form.get('stock') as FormArray;
control.removeAt(index);
}
Upvotes: 2
Reputation: 11
that's a function that removes an element from an array according to the example that I found in this link https://www.cnblogs.com/Answer1215/p/6592580.html a formgroup is created by means of an array
removeStock ({group, index}: {group: FormGroup, index: number}) {
const control = this.form.get ('stock') as FormArray;
control.removeAt (index);
}
two parameters are passed which are the class and the identifier
{group, index}
here the data type of the parameters is expressed
{group: FormGroup, index: number}
a constant is declared that contains the total of the elements const control = this.form.get ('stock') as FormArray;
it removes the element identified by index
control.removeAt (index);
XD That's all I speak Spanish so excuse my writing done on google XD
Upvotes: 1