【发布时间】:2019-03-16 04:21:28
【问题描述】:
简要说明:我已使用**js** 获取变量res 中的结果。
res 在控制台上的结果如下所示。
要求:我想在角度变量中获取
res的值。我已经声明了
resarry = [];
当我这样做时
this.resarry = res;
console.log(this.resaary);
出现错误 - 无法设置“resarray”的属性未定义。
console.log(results); // no problem in this line
console.log(this.resarry); // giving error
export class HomePage {
resarry = [];
constructor(){
var connection = new JsStore.Instance();
var dbName = 'Demo';
connection.openDb(dbName);
connection.select({
from: Test1,
}).then(function(res) {
// results will be array of objects
console.log(res,'results');
this.resarry = results;
console.log(results); // no problem in this line
console.log(this.resarry); // giving error
}).catch(function(err) {
console.log(err, 'error');
alert(err.message);
});
}
}
【问题讨论】:
-
如果你使用
function(res)这个范围是丢失使用箭头函数(res) => {}你不会丢失 -
试试
resarry: any;和constructor(){ this.resarry={}} -
console.log(this.resaary) 或 console.log(this.resarray);; ?
标签: javascript angular ionic-framework ionic2 ionic3