编程语言
首页 > 编程语言> > javascript – 如何在角度2中正确使用forEach循环?

javascript – 如何在角度2中正确使用forEach循环?

作者:互联网

我是Angular2的新手,在这里我试图循环遍历数组“mmEditorTypes”并检查条件,如果条件满足,那么我将执行“open方法”.

但每当我执行以下代码时,我收到此错误:

portalModeService:portalMode的加载抛出异常:

TypeError: Cannot read property ‘forEach’ of undefined”.

有人能让我知道如何解决这个错误吗?

porta.service.ts:

let mmApplicationType = this.applicationTypes['mm.application'];
    mmEditorTypes = [
      {
        type: mmApplicationType,
        payload: {
          'mm.application': 'mm.application'
        }
      }
    ];


mmEditorTypes.forEach(function(editor)  {
          if (!isAppContained(viewState, editor.type)) {
            this.applicationLifeCycleService.open(editor.type, editor.payload);
          }
});

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;
    }

解决方法:

你不能将angular.forEach与angular一起使用,它与angularjs一起使用.

使用

for (let appViewState of viewState.appViewStates) {
   if (appViewState.signature.appType === appType) {
          isContained = true;
    }
}

标签:javascript,angular,foreach,angular2-services
来源: https://codeday.me/bug/20190724/1521990.html