[英]function that returns data from an observableArray in knockout
this is the case... 就是这样
i have a query with this format 我有这种格式的查询
{
query:'A query string',
user: 'email@user.com.ar',
}
in my view model i have a list of users and a function 在我的视图模型中,我有一个用户列表和一个功能
var self=this;
self.queries=ko.observableArray(
[
{
query:'A query string',
user: 'email@user.com.ar',
}
]);
self.users=ko.observableArray(
[
{user:'email@user.com.ar',data_of_user:{name:'User Name'}}
])
self.getUserInfo=function(email)
{
var data_user=_.findWhere(self.users(),{email:email});
return data_user.data_of_user.name;
}
but didnt work 但没有工作
how can i do this but with other approach?? 除了其他方法,我该怎么做?
i need to create a span that can pass the email of the user and get the name 我需要创建一个可以通过用户电子邮件并获得名称的跨度
<div data-bind="foreach:queries">
<span data-bind="text:$root.getUserInfo.bind($data,$data.email)">
<div/>
There were some erros I fxed. 我固定了一些错误。
View : 查看:
<div data-bind="foreach:queries">
<div data-bind="with: ($root.getUserInfo($data))">
<!-- user context -->
<span data-bind="text: email"></span>
<span data-bind="text: data_of_user.name"></span>
</div>
<div/>
JS : JS:
var VM = function () {
var self = this;
self.queries = ko.observableArray(
[{
query: 'A query string',
email: 'email@user.com.ar',
}]);
self.users = ko.observableArray(
[{
email: 'email@user.com.ar',
data_of_user: {
name: 'User Name'
}
}])
self.getUserInfo = function (user) {
var user = _.findWhere(self.users(), {
'email': user.email
});
return user;
}
;
};
ko.applyBindings(new VM());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.