2017-04-03 91 views
1

我想遍歷我可愛的Angular/Typescript代碼中的對象列表,但由於某種原因,它甚至沒有嘗試這樣做。這裏的代碼:Angular for each not

businessList: RemoteDataSet<BusinessModel>; 
businessModel: BusinessModel; 

this.businessList.forEach(bl => { 
      console.log("in foreach"); 
      if (bl.id == this.user.businessId) { 
       console.log("they are equal"); 
       this.businessModel = bl; 
      } 
      else { 
       console.log("They are not equal"); 
      } 
     }); 

我已驗證this.businessList有數據(約40項)。即使如此,它也不會迭代一次。對於Angular和Typescript,我顯然很新,但這似乎是正確的。我錯過了什麼?

+3

發佈一個完整的最小范例來重現問題。特別是發佈RemoteDataSet的代碼及其forEach()方法。我們無法猜測它是如何實現的。 –

+1

如果你還可以發佈你的JSON'BusinessModel'對象的樣本,這將是非常有用的。 –

+0

您錯誤地調用了角度foreach循環。 angular.forEach(businessList => {...} 就是它應該是的。 檢查此前面的問題:http://stackoverflow.com/questions/29953198/foreach-loop-in-angularjs – DDelgro

回答

1

也許試試angular.forEach方法?

forEachFunction =() => { 
     angular.forEach(this.businessList, (value, key) => { 
      if (value.id == this.user.businessId) { 
       console.log("they are equal"); 
       this.businessModel = value; 
      } 
      else { 
       console.log("They are not equal"); 
      } 
     }); 
    };