Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在aurelia repeat for中将字符串转换为表达式(值)?_Javascript_Aurelia_Aurelia Templating - Fatal编程技术网

Javascript 如何在aurelia repeat for中将字符串转换为表达式(值)?

Javascript 如何在aurelia repeat for中将字符串转换为表达式(值)?,javascript,aurelia,aurelia-templating,Javascript,Aurelia,Aurelia Templating,重复for循环中使用的数组 let loopArr = ["item.name + ' /'+ item.DisplayName? item.DisplayName: item.otherDisplayName", "item.description + ' /'+ item.anotherDescription"] 模板 <div repeat.for = item of data">

重复for循环中使用的数组

let loopArr = ["item.name + ' /'+ item.DisplayName? item.DisplayName: item.otherDisplayName", 
                    "item.description + ' /'+ item.anotherDescription"]
模板

<div repeat.for = item of data">
    <div repeat.for = "row of loopArr">
        <span textcontent.bind="renderRow(row, item)></span>
    </div>
</div>
<div repeat.for = item of data">
    <div repeat.for = "row of loopArr">
        <span>${item.name + ' /'+ item.DisplayName? item.DisplayName: item.otherDisplayName} </span>
        <span>${item.description + ' /'+ item.anotherDescription} </span>
    </div>
</div>
实际上我想在模板中显示如下所示

<div repeat.for = item of data">
    <div repeat.for = "row of loopArr">
        <span textcontent.bind="renderRow(row, item)></span>
    </div>
</div>
<div repeat.for = item of data">
    <div repeat.for = "row of loopArr">
        <span>${item.name + ' /'+ item.DisplayName? item.DisplayName: item.otherDisplayName} </span>
        <span>${item.description + ' /'+ item.anotherDescription} </span>
    </div>
</div>

我不知道为什么要以字符串格式添加逻辑并使用
eval
。您可以直接将其添加到
模板
并显示:


${item.name+'/'+(item.DisplayName?item.DisplayName:item.otherDisplayName)}
${item.description+'/'+item.anotherDescription}

假设您有一个自定义字符串格式的列表,并且正在从另一个文件导入它们。您可以创建函数的数组,而不是字符串数组。这是一种比运行
eval

displayTemplates=[
item=>item.name+'/'+(item.DisplayName?item.DisplayName:item.otherDisplayName),
item=>item.description+'/'+item.anotherDescription
] 
然后在
模板中


${func(项目)}

此外,字符串格式中存在逻辑错误<代码>+
运算符与三值运算符进行了比较

所以

item.name+'/'+item.DisplayName?item.DisplayName:item.otherDisplayName
实际评估为

(item.name+'/'+item.DisplayName)?item.DisplayName:item.otherDisplayName
因此,此表达式将始终计算为
item.DisplayName
,因为
item.name+'/'+item.DisplayName
将永远不会被使用

您需要在三元操作周围添加
()

item.name+'/'+(item.DisplayName?item.DisplayName:item.otherDisplayName)
//或
item.name+'/'+(item.DisplayName??item.otherDisplayName)

您的问题不清楚(至少对我来说)。您能给我们一个您正在使用的示例数据集,以及您希望在渲染后获得的html结果吗?一个起点也不错,我们可以从这个起点着手解决问题谢谢你的回答,我现在得到了解决方案,下次我将添加一个工作示例