我有json数据从外部api。
ChangePercent {{股票。changePercent}} % < / span > < / h4 >
上面的代码是获得响应数据json和它的返回与数字类型和使用模板引擎express手柄渲染。它的工作但是。 。
我想改变字体颜色红色如果股票。changePercent & lt;0绿色的股票。changePercent祝辞0
###你可以在handlebarsjs中有条件地渲染
模板:
<h4>ChangePercent <span class="badge bg-secondary" {{#if stockChangeSmallerThanZero}}style="color: red;"{{/if}}> {{ stock.changePercent}} % </span></h4>
变量
{
stockChangeSmallerThanZero: true,
}
在线操场上:https://handlebarsjs。com/playground。html#format=1¤tExample=%7B%22template%22%3A%22%3Ch4%3EChangePercent%20%3Cspan%20class%3D%5C%22badge%20bg-secondary%5C%22%20%7B%7B%23if%20stockChangeSmallerThanZero%7D%7Dstyle%3D%5C%22color%3A%20red%3B%5C%22%7B%7B%2Fif%7D%7D%3E%20%20%7B%7B%20stock。changePercent%7D%7D%20%25%20%20%20%20%3C%2Fspan%3E%3C%2Fh4%3E% 20% 22% 2 c % 22泛音% 3 22% % 5 b % 5 d % 2 c % 22输入% 22% 3 7 b % 5 cn % % 22% 20% 20 stockchangesmallerthanzero % 3 a % 20真正% 2 c % 5 cn % 7 d % 5 cn % 22% 2 c % 22输出% 3 22% % 3 ch4 % 3 echangepercent % 20% 22% 3 cspan % 20类% 3 d % 5 c % 22徽章% 20 bg-secondary % 5 c % 20风格22% % 3 d % 5 c % 22 % % 20 3红色% 3 b % 5 c % 3 e % 20% 22% 20% 20% 25% 20% 20% 20% 20% 3 c % 2 fspan % 3 e % 3 c % 2 fh4 % 3 e % 22% 2 c % 22 preparationscript % 22% 20% % 22% 22% 2 c % 22 handlebarsversion % 22% 3 22% % 224。7。7 % 7 d
<h4>ChangePercent <span class="badge bg-secondary" {{#if stockChangeSmallerThanZero}}style="color: red;"{{/if}}> {{ stock.changePercent}} % </span></h4>
{
stockChangeSmallerThanZero: true,
}