【发布时间】:2015-12-06 00:48:23
【问题描述】:
我在查看我的代码有什么问题时遇到了一些麻烦。更可能与 Knockout.js 部分......它给了我以下错误:
消息:无法处理绑定“attr: function (){return {href:website()} }”
HTML
<div class="demo-card-square mdl-card mdl-shadow--2dp" data-bind="foreach: favoriteSpot">
<div class="mdl-card__title mdl-card--expand">
<h2 class="mdl-card__title-text">Update</h2>
</div>
<div class="mdl-card__supporting-text" data-bind="text:name"></div>
<div class="mdl-card__supporting-text" data-bind="text:location"></div>
<a data-bind="attr: {href: website()}">Website</a>
</div>
JS
var favoriteSpotsList = [{
venueName: "name",
venueLocation: "address",
website: "url",
image: "<img src='img'",
}];
var favoriteSpot = function(data) {
this.name = ko.observable(data.venueName);
this.address = ko.observable(data.venueLocation);
this.website = ko.observable(data.website);
this.image = ko.observable(data.img);
};
var AppViewModel = function() {
var self = this;
/* Create array of hotspot locations. */
this.hotSpotList = ko.observableArray([]);
favoriteSpotsList.forEach(function(spot) {
self.hotSpotList.push(new favoriteSpot(spot));
});
};
ko.applyBindings(new AppViewModel());
【问题讨论】:
-
foreach: favoriteSpot ???你的意思是 hotSpotList , foreach 迭代数组
-
您的视图模型上似乎没有
favoriteSpot属性。你的意思是hotSpotList? -
谢谢各位大神!长时间盯着屏幕。
标签: javascript html knockout.js