If statement Knockout.js If语句问题
我想检查我的“类型”绑定,如果他等于某个字符串,但看起来他没有执行它 我的html页面:If statement Knockout.js If语句问题,if-statement,knockout.js,If Statement,Knockout.js,我想检查我的“类型”绑定,如果他等于某个字符串,但看起来他没有执行它 我的html页面: <div class="socialWrapper" data-bind="foreach: facebookposts"> <!-- ko if: {Type()==='link'}--> <img data-bind='attr: { src: FromPicture }'/> <p data-bind="
<div class="socialWrapper" data-bind="foreach: facebookposts">
<!-- ko if: {Type()==='link'}-->
<img data-bind='attr: { src: FromPicture }'/>
<p data-bind="text:From"></p>
<!-- /ko -->
</div>
有人知道如何修复这个if语句吗?更新了整个视图模型 我会在你的viewmodel上做一个计算的可观察的,看起来像这样
function Post(allData) {
var profileImageUrl = "http://graph.facebook.com/" + allData.from.id + "/picture?type=large";
this.Type = ko.observable(allData.type);
this.From = ko.observable(allData.from.name);
this.FromPicture = ko.observable(profileImageUrl);
this.Created = ko.observable(allData.created_time);
this.Comments = ko.observable(allData.comments.count);
this.Message = "";
this.Likes = "";
this.LinkImage = "";
this.LinkUrl = "";
this.LinkName = "";
this.LinkTitle = "";
this.LinkDescription = "";
this.Story = "";
this.Photo = "";
this.PhotoDescription = "";
var self = this;
this.isLink = ko.computed(function() {
return self.Type() === 'link';
});
if (allData.type === 'status') {
this.Message = ko.observable(allData.message);
this.Likes = ko.observable(allData.likes);
}
if (allData.type === 'link') {
this.Message = ko.observable(allData.message);
this.LinkImage = ko.observable(allData.picture);
this.LinkUrl = ko.observable(allData.link);
this.LinkName = ko.observable(allData.name);
this.LinkTitle = ko.observable(allData.caption);
this.LinkDescription = ko.observable(allData.description);
this.Likes = ko.observable(allData.likes);
}
if (allData.type === 'photo') {
this.Story = ko.observable(allData.story);
this.Photo = ko.observable(allData.picture);
this.PhotoDescription = ko.observable(allData.description);
}
}
然后
就个人而言,我喜欢尽可能保持我的视图干净,这意味着没有javascript计算。更好地分离关注点。我认为您需要使用$parent上下文绑定。试试这个-
<div class="socialWrapper" data-bind="foreach: facebookposts">
<!-- ko if: $parent.Type === 'link'-->
<img data-bind='attr: { src: FromPicture }'/>
<p data-bind="text:From"></p>
<!-- /ko -->
</div>
你说的“不工作”是指它似乎总是被认为是正确的吗?总是假的?或者它会给您带来一些错误?另外,请尝试键入()。可观察对象实际上是函数,您需要括号来调用函数并实际获取值。@Matt Burland:它的计算结果始终为false,Type()='link'不起作用。如果我尝试获取错误:未捕获错误:无法解析绑定。消息:SyntaxError:意外标记。;绑定值:如果:{$parent.Type()==='link'}。我也得到了这个没有{}括号的结果。我在IE或FF中没有看到错误。你能提供一个facebookposts结构的例子吗?我用Chrome来测试它。但是现在已经修好了,谢谢你的帮助!令人惊叹的!是的,如果你没有忘记一个可观察对象的()代码,你就不会是一个真正的knockoutjs开发人员。
function Post(allData) {
var profileImageUrl = "http://graph.facebook.com/" + allData.from.id + "/picture?type=large";
this.Type = ko.observable(allData.type);
this.From = ko.observable(allData.from.name);
this.FromPicture = ko.observable(profileImageUrl);
this.Created = ko.observable(allData.created_time);
this.Comments = ko.observable(allData.comments.count);
this.Message = "";
this.Likes = "";
this.LinkImage = "";
this.LinkUrl = "";
this.LinkName = "";
this.LinkTitle = "";
this.LinkDescription = "";
this.Story = "";
this.Photo = "";
this.PhotoDescription = "";
var self = this;
this.isLink = ko.computed(function() {
return self.Type() === 'link';
});
if (allData.type === 'status') {
this.Message = ko.observable(allData.message);
this.Likes = ko.observable(allData.likes);
}
if (allData.type === 'link') {
this.Message = ko.observable(allData.message);
this.LinkImage = ko.observable(allData.picture);
this.LinkUrl = ko.observable(allData.link);
this.LinkName = ko.observable(allData.name);
this.LinkTitle = ko.observable(allData.caption);
this.LinkDescription = ko.observable(allData.description);
this.Likes = ko.observable(allData.likes);
}
if (allData.type === 'photo') {
this.Story = ko.observable(allData.story);
this.Photo = ko.observable(allData.picture);
this.PhotoDescription = ko.observable(allData.description);
}
}
<div class="socialWrapper" data-bind="foreach: facebookposts">
<!-- ko if: isLink -->
<img data-bind='attr: { src: FromPicture }'/>
<p data-bind="text:From"></p>
<!-- /ko -->
</div>
<div class="socialWrapper" data-bind="foreach: facebookposts">
<!-- ko if: $parent.Type === 'link'-->
<img data-bind='attr: { src: FromPicture }'/>
<p data-bind="text:From"></p>
<!-- /ko -->
</div>