Angular ForEach in Angular4 / Typescript?

I see a lot of answers about using ngFor when I search for it, but I understand ngFor. I am asking about the angular.forEach() constructor, which is used in my Angular 1 controllers. They are flagged as errors in TS and do not compile.

For example, I have one with a nested loop:

  _this.selectChildren = function (data, $event) { var parentChecked = data.checked; angular.forEach(_this.hierarchicalData, function (value, key) { angular.forEach(value.children, function (value, key) { value.checked = parentChecked; }); }); }; 

What does this design look like in Typescript for Angular 4?

+14
source share
4 answers

in angular4 foreach like this. try it.

  selectChildren(data, $event) { let parentChecked = data.checked; this.hierarchicalData.forEach(obj => { obj.forEach(childObj=> { value.checked = parentChecked; }) }; } 
+31
source

you can try typescript For :

 selectChildren(data , $event){ let parentChecked : boolean = data.checked; for(let o of this.hierarchicalData){ for(let child of o){ child.checked = parentChecked; } } } 
+9
source
 arrayData.forEach((key : any, val: any) => { key['index'] = val + 1; arrayData2.forEach((keys : any, vals :any) => { if (key.group_id == keys.id) { key.group_name = keys.group_name; } }) }) 
+1
source

In Typescript, use For Each as shown below.

 selectChildren(data, $event) { let parentChecked = data.checked; for(var obj in this.hierarchicalData) { for (var childObj in obj ) { value.checked = parentChecked; } } } 
0
source

Source: https://habr.com/ru/post/1268776/


All Articles