Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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 如何在handlebar.js中比较循环中的两个属性,其中一个属性不在循环范围内?_Javascript_Html_Handlebars.js - Fatal编程技术网

Javascript 如何在handlebar.js中比较循环中的两个属性,其中一个属性不在循环范围内?

Javascript 如何在handlebar.js中比较循环中的两个属性,其中一个属性不在循环范围内?,javascript,html,handlebars.js,Javascript,Html,Handlebars.js,我使用handlebar.js根据字符串是否匹配当前用户的用户名来显示元素 实际上,我正在循环浏览一个集合blogs,这就是属性author发挥作用的地方。无论如何,author.username代表一些用户名字符串,我可以使用它。我的问题是,如果我将user.attributes.username更改为某个预定义的字符串,即“martin”,那么该条件将签出,并以我想要的方式显示元素。但是我还有一个属性user.attributes.username,它在#ifEquals条件之外呈现得很好。

我使用handlebar.js根据字符串是否匹配当前用户的用户名来显示元素

实际上,我正在循环浏览一个集合
blogs
,这就是属性
author
发挥作用的地方。无论如何,
author.username
代表一些用户名字符串,我可以使用它。我的问题是,如果我将
user.attributes.username
更改为某个预定义的字符串,即“martin”,那么该条件将签出,并以我想要的方式显示元素。但是我还有一个属性
user.attributes.username
,它在
#ifEquals
条件之外呈现得很好。我真的想将
author.username
与这个属性进行比较,而不是一些预定义的字符串

我如何比较两个属性,即
author.username
user.attributes.username
而不是
author.username
“martin”

author.username
user.attributes.username
具有相同的值。这对我来说现在不起作用,但这是我最终想要的:

{{#each blog}}
        {{#ifEquals author.username user.attributes.username}}
            <a href="#" onClick="return false;"><span class="delete-container" style="cursor: pointer" data-blog-id="{{objectId}}"><span class="typcn typcn-arrow-sorted-down hvr-pulse-grow" style="color: gray; font-size: 30px;"></span></span></a>
        {{/ifEquals}}
{{/each}}
{{#每个博客}
{{#ifEquals author.username user.attributes.username}
{{/ifEquals}
{{/每个}}
这是可行的,但不足以满足我的目的:

{{#each blog}}
        {{#ifEquals author.username "martin"}}
            <a href="#" onClick="return false;"><span class="delete-container" style="cursor: pointer" data-blog-id="{{objectId}}"><span class="typcn typcn-arrow-sorted-down hvr-pulse-grow" style="color: gray; font-size: 30px;"></span></span></a>
        {{/ifEquals}}
{{/each}}
{{#每个博客}
{{{#ifEquals author.username“martin”}
{{/ifEquals}
{{/每个}}
handlebar.js帮助程序:

<script>
    Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
        return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
    });
</script>

把手.注册表帮助器('ifEquals',函数(arg1,arg2,选项){
return(arg1==arg2)?options.fn(this):options.inverse(this);
});

编辑:进一步检查后,问题似乎是
user.attributes.username
不会在
blog
循环中呈现。它认为
user
blog
的一个属性。是否有任何方法可以访问
用户
,就好像它不在循环范围内一样?

显然,您可以通过附加
来访问handlebar.js中的外部循环属性。

{{currentUser.username}}//在循环外呈现良好

 {{#each blog}}
    // Append ../ to access currentUser inside the loop
    {{#ifEquals author.username ../currentUser.username}}
        // Show element here
    {{/ifEquals}}
 {{/each}}

。/user
是一个来自
blog
循环外部的属性,而
author
属于
blog

您是否使用
ember.js
?否。意外添加了标记。谢谢移除它。