Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use forEach loop correctly in angular 2?

I am new to Angular2 and here I am trying to loop through the array "mmEditorTypes" and checking the condition, if the condition is satisfied then I'll be executing the "open method".

But whenever i execute the below code, I am getting this error :

portalModeService: loading of portalMode threw exception:

TypeError: Cannot read property 'forEach' of undefined".

Could someone let me know how to fix this error?

porta.service.ts :

function isAppContained(viewState, appType){
      let angular:any;
      let isContained = false;
      angular.forEach(viewState.appViewStates, function (appViewState) {
        if (appViewState.signature.appType === appType) {
          isContained = true;
        }
      });
      return isContained;
    }
like image 833
Vivek Avatar asked Dec 04 '22 20:12

Vivek


2 Answers

As @Sajeetharan said you can't use angular.forEach in angular 2+

So you can use simple foreach in typescript like :

var someArray = [1, 2, 3];
someArray.forEach((item, index) => {
  console.log(item); // 1, 2, 3
  console.log(index); // 0, 1, 2
});
like image 116
Javascript Hupp Technologies Avatar answered Dec 28 '22 20:12

Javascript Hupp Technologies


You cannot use angular.forEach with angular , its with angularjs.

use

for (let appViewState of viewState.appViewStates) {
   if (appViewState.signature.appType === appType) {
          isContained = true;
    }
}
like image 23
Sajeetharan Avatar answered Dec 28 '22 18:12

Sajeetharan