0
角分量从解析JSON和在阵列返回它的JavaScript文件接收阵列。但是,我有一个问题,模板中的组件不能识别变量jsonArray:any
,它表示它是未定义的。如何推JavaScript数组到一个数组中角
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import '../assets/js/jsonReader.js'
declare var myExtObject: any;
declare var webGlObject: any;
@Component({
selector: 'app-root',
template: `
<div>
{{jsonArray.name}}
</div>
`,
})
export class AppComponent {
test: Object[];
jsonArray:any;
output = [];
constructor() {
this.jsonArray = webGlObject.init();
}
}
这是这是JavaScript代码
var webGlObject = (function() {
return {
init: function() {
getJsonArryObject();
}
}
})(webGlObject || {})
function getJsonArryObject() {
loadJSON(function (response) {
// Parse JSON string into object
var js = JSON.parse(response);
});
//return getObjects(js, key, val);
}
这是JSON文件
[
{
"name": "ar",
"hasTributaryInfo": true,
"hasTributaryInfoMandatory": false,
"isRazonSocialMandatory": true,
"isRUCMandatory": false,
"array": [
1,
2,
3
]
},
{
"name": "sa",
"hasTributaryInfoMandatory": true,
"hasTributaryInfo": true,
"isRazonSocialMandatory": true,
"isRUCMandatory": true,
"object": {
"a": "b",
"c": "d",
"e": "f"
}
},
{
"name": "co",
"hasTributaryInfoMandatory": false,
"hasTributaryInfo": false,
"isRazonSocialMandatory": false,
"isRUCMandatory": false
}
]
你的初始化函数不返回任何东西,所以当你做'this.jsonArray = webGlObject.init();'你将this.jsonArray设置为undefined。 – James
loadJSON()是做什么的?它是一个库还是你自己的实现? – andreim