无法获取变量中的数组值 - angular
Cannot fetch array value in variable - angular
简要说明: 我使用 **js**
.
在变量 res
中获取了结果
res
在控制台上的结果如下所示。
see here
Requirement: I want to get the value of res
in angular variable. I have declared
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);
});
}
}
resarry:number[] = new Array();
将初始化一个未定义的空数组。
您可以设置预期结果的类型。
变化:
connection.select({
from: Test1,
}).then(function(res) {
// ...
});
至:
connection.select({
from: Test1,
}).then(res => {
// ...
});
基本上,function() { ... }
无法从外部作用域访问 this
,而箭头函数可以。可以找到更深入的解释 here.
connection.select({
from: Test1,
}).then(function(res) { // results will be array of objects
console.log(res,'results');
this.resarry = results; // perhaps like that => this.resarry = res
console.log(results); // i think, you should have an error on this line because **results** isn't a variable but a string in your console
console.log(this.resarry); // giving error
})
因为,"this" 意味着是当前函数对象。所以你在构造函数中使用的 "this" 并不是实际的组件
使用箭头函数或
constructor(){
var connection = new JsStore.Instance();
var dbName = 'Demo';
connection.openDb(dbName);
var $this = this;
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);
});
}
用户箭头函数以及打字稿类型在分配之前验证事物
export class HomePage {
resarry: any[] = []; //--> resarry: any[] -> used to set the type as array
constructor() {
let connection = new JsStore.Instance(); //-> use Let insted of Var
let dbName = 'Demo';
connection.openDb(dbName);
connection.select({
from: "Test1",
}).then(res => {
console.log(res);
if (res)
if (res.length > 0){
this.resarry = res;
console.log(this.resarry);
console.log("connection Successful with array objects");
}else{
console.log("connection Successful without array objects");
}
}), err => {
console.log("connection error");
};
}
}
简要说明: 我使用 **js**
.
res
中获取了结果
res
在控制台上的结果如下所示。
see here
Requirement: I want to get the value of
res
in angular variable. I have declared
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);
});
}
}
resarry:number[] = new Array();
将初始化一个未定义的空数组。
您可以设置预期结果的类型。
变化:
connection.select({
from: Test1,
}).then(function(res) {
// ...
});
至:
connection.select({
from: Test1,
}).then(res => {
// ...
});
基本上,function() { ... }
无法从外部作用域访问 this
,而箭头函数可以。可以找到更深入的解释 here.
connection.select({
from: Test1,
}).then(function(res) { // results will be array of objects
console.log(res,'results');
this.resarry = results; // perhaps like that => this.resarry = res
console.log(results); // i think, you should have an error on this line because **results** isn't a variable but a string in your console
console.log(this.resarry); // giving error
})
因为,"this" 意味着是当前函数对象。所以你在构造函数中使用的 "this" 并不是实际的组件
使用箭头函数或
constructor(){
var connection = new JsStore.Instance();
var dbName = 'Demo';
connection.openDb(dbName);
var $this = this;
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);
});
}
用户箭头函数以及打字稿类型在分配之前验证事物
export class HomePage {
resarry: any[] = []; //--> resarry: any[] -> used to set the type as array
constructor() {
let connection = new JsStore.Instance(); //-> use Let insted of Var
let dbName = 'Demo';
connection.openDb(dbName);
connection.select({
from: "Test1",
}).then(res => {
console.log(res);
if (res)
if (res.length > 0){
this.resarry = res;
console.log(this.resarry);
console.log("connection Successful with array objects");
}else{
console.log("connection Successful without array objects");
}
}), err => {
console.log("connection error");
};
}
}