【发布时间】:2018-10-15 18:19:45
【问题描述】:
我正在开发一个 node.js 应用程序。我在 app.js 中加载了一个 JSON 文件作为 app.locals.parameter。 然后我试图在我的 index.hbs 中使用它。
假设:
app.locals.componentData = require('./myfile.json');
当我在下面的 HTML 部分中使用参数时,它运行正常。
{{#each componentData.categories}}
<li id="{{this.categoryName}}" name="{{this.categoryName}}" class="btn btn-primary btn-sm" style="position: relative; z-index: 10; margin-bottom:3px" draggable="true" role="option" aria-grabbed="false"><div><img src="images/plugin.png" alt="{{this.categoryDescription}}"/>{{this.categoryName}}</div></li>
{{/each}}
但是当我尝试在我的 javascript 函数中使用该参数时,它无法识别它。
function InitComponents() {
for( var i = 0; i < componentData.categories.length; i++ ){
alert("in loop!"+i+"-"+componentData.categories[i].categoryName);
if(componentData.categories[i].categoryName.match(itemId)){
alert("here:"+componentData.categories[i].parameters.length);
createParameterList(newID,componentData.categories[i].parameters);
}
}
}
我的 JSON 格式如下:
{
"categoryName": "xyz",
"categories": [
{
"categoryName": "ABC",
"parameters": [
{
"ParameterID": "ID",
"ParameterName": "ID",
"ParameterDefultValue": "",
"ParameterValue": "",
"ParameterDescription": "ID Description"
}]
}]
}
我可以知道为什么系统无法理解吗 componentData.categories.length 在我的脚本部分?
【问题讨论】:
标签: javascript html node.js json