Angularjs 传至角度ForEach时,返回Rx值不可用
我订阅了一个Rx返回,并将其传递给Angular.ForEach循环,但它的值未定义。但是,我设法在console.log print上获得了结果 下面是调用typescript的代码Angularjs 传至角度ForEach时,返回Rx值不可用,angularjs,typescript,rxjs,Angularjs,Typescript,Rxjs,我订阅了一个Rx返回,并将其传递给Angular.ForEach循环,但它的值未定义。但是,我设法在console.log print上获得了结果 下面是调用typescript的代码 this.wordPressBlogPostRepository.getPortfolios().subscribe( (result) => { this.stopLoading(); this.viewmodels = result; consol
this.wordPressBlogPostRepository.getPortfolios().subscribe(
(result) => {
this.stopLoading();
this.viewmodels = result;
console.log(this.viewmodels);
angular.forEach(this.viewmodels, function (value, key) {
this.viewmodels[key].title.rendered = value.title.rendered.replace("Softinn Portfolios: ", "");
});
},
(error) => {
swal({
title: 'Error',
text: error,
type: "error"
});
this.stopLoading();
}
);
以下是错误消息:
未捕获的TypeError:无法读取未定义的属性“viewmodels”
在角度控制器处?v=RVYJL6curxt7T6FM4ZFRTROU0C-mN9uPa6W1ArXQ9301:1
at Object.t[作为forEach]
如果我改为这样做,它会起作用
this.wordPressBlogPostRepository.getPortfolios().subscribe(
(result) => {
this.stopLoading();
this.viewmodels = result;
console.log(this.viewmodels);
for (var blogCounter = 0; blogCounter < this.viewmodels.length; blogCounter++) {
this.viewmodels[blogCounter].title.rendered = this.viewmodels[blogCounter].title.rendered.replace("Portfolios: ", "");
}
},
(error) => {
swal({
title: 'Error',
text: error,
type: "error"
});
this.stopLoading();
}
);
我错过了什么?我应该使用complete而不是result吗?这很可能是因为forEach函数中的上下文 您可以尝试:
angular.forEach(this.viewmodels, (value, key) => {
this.viewmodels[key].title.rendered = value.title.rendered.replace("Softinn Portfolios: ", "");
});
由于fat arrow函数保持了此