2017-04-03 88 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"); 
      } 
     }); 
    };