无法使用rivetJs显示JSON对象数组中的值?

时间:2021-11-29 12:21:26

I am working on a single page application and I am using Rivet.js library to bind the data that I fetch from the server to the DOM elements to make the templating of the data easier.

我正在处理单页面应用程序,我正在使用Rivet.js库将我从服务器获取的数据绑定到DOM元素,以便更轻松地模拟数据。

$.getJSON('/auth/user', function(response){
            console.log(response);
            console.log(response.roles[0].name);
            rivets.bind($('#profile'), {user: response});
        });

Now I can easily access properties in the DOM like { user.first_name } or { user.last_name }

现在我可以轻松访问DOM中的属性,如{user.first_name}或{user.last_name}

The response contains an array of role objects for the user. When I tried logging it to the console, like console.log(response.roles[0].name);, it worked fine, but when I tried { user.roles[0].name } in the DOM, it is not working. What might I be doing wrong?

响应包含用户的角色对象数组。当我尝试将其登录到控制台时,例如console.log(response.roles [0] .name);,它工作正常,但是当我在DOM中尝试{user.roles [0] .name}时,它不是工作。我可能做错了什么?

1 个解决方案

#1


This might be exactly what you are looking for: binding arrays in rivets

这可能正是您所寻找的:铆钉中的绑定数组

How to print an array in rivets.js

如何在rivets.js中打印数组

#1


This might be exactly what you are looking for: binding arrays in rivets

这可能正是您所寻找的:铆钉中的绑定数组

How to print an array in rivets.js

如何在rivets.js中打印数组