Angular Foreach in Angular4/Typescript?
I See Many Answers About Using ngFor When I Search for This, but I Understand ngFor. I'm Asking About the Angular. forEach() Constructor Used in My Angular 1...
I see many answers about using ngFor when I search for this, but I understand ngFor. I'm asking about the angular.forEach() constructor used in my Angular 1 controllers. These 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 construct look like in Typescript for Angular 4?
4 Answers
in angular4 foreach like that. try this.
selectChildren(data, $event) {
let parentChecked = data.checked;
this.hierarchicalData.forEach(obj => {
obj.forEach(childObj=> {
value.checked = parentChecked;
});
});
}
you can try typescript's For :
selectChildren(data , $event){
let parentChecked : boolean = data.checked;
for(let o of this.hierarchicalData){
for(let child of o){
child.checked = parentChecked;
}
}
}
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;
}
})
})
In Typescript use the For Each like below.
selectChildren(data, $event) {
let parentChecked = data.checked;
for(var obj in this.hierarchicalData)
{
for (var childObj in obj )
{
value.checked = parentChecked;
}
}
}