I knew this question may have been asked before but believe most thread I search here does not address my issue.
I have angularjs code that I want to move to angular 6. All I want is to implement the Foreach loop in angular6 judging from angularjs code below
Angularjs code
setTimeout(function() {
$scope.$apply(function(){
angular.forEach(res.data,function(item) {
$scope.crud_s.push(item);
});
});
},500);
This is what I have tried in angular6
getRecord(): void {
this.crudService.getAll().subscribe(
res => {
this.crud_s = res;
// starting code issue
setTimeout(function()=>{
this.$apply(function(){
angular.forEach(this.crud_s,function(item) {
this.crud_s.push(item);
});
});
},500);
//end code issue
//more codes will continues
}
$scope/$apply. There arechangeDetectorreferences. But do you really need that? If there's asetTimeout()there will an auto change detection by angular itself. Also you will loosethisreference if you are using functions. Please seearrowfunctions of ES6.res.data? Response from an HTTP request? Plain Array?for(let eachKey in res.data) {}can use this for both arrays and objects