我的任務是形式的「href的」的鏈接,每次一個附加的可觀測改爲動態地。這裏是鏈接中的示例: JS Fiddle example link與傳遞的參數ko.computed顯示功能,但沒有一個值
實現這一目標時,我遇到了兩個問題:
當我試圖通過一些字符串+計算觀察到的,我得到計算 功能列表,而不是它的價值。
<a data-bind="attr: {href : '#someHash/' + getHref(10)}">Link</a>
鏈接看起來像:
http://fiddle.jshell.net/3DAfQ/1/show/#someHash/function h(){if(0<arguments.length)return"function"===typeof v?v.apply(d,arguments):j(Error("Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.")),this;n||g();b.r.Wa(h);return l}
,我找不到甚至接近爲宜。
第二,當我嘗試更改可觀察值時,計算取決於鏈接不會更改。
<a href="#" data-bind="click: changeStoreHref(20)">change Link</a> self.changeStoreHref = function(num) { self.storeHref(num); };
下面是HTML代碼:
<a data-bind="attr: {href : '#someHash/' + getHref(10)}">Link</a>
<a href="#" data-bind="click: changeStoreHref(20)">change Link</a>
而且knockoutjs:
function viewModel()
{
var self = this;
self.storeHref = ko.observable('ten');
self.getHref = function(id)
{
return ko.computed({
read: function()
{
self.storeHref(id);
return self.storeHref();
}
});
};
self.changeStoreHref = function(num)
{
self.storeHref(num);
};
}
ko.applyBindings(new viewModel());
我提醒,你可以檢查以下鏈接這個例子:JS Fiddle example link 感謝。
非常感謝,你真的幫助。並感謝你提供了非常有用的信息。 – Kamilius 2013-04-26 11:36:49
感謝您提供一些示例代碼,其中包含單獨的輸出值。我有一個類似的問題,因爲我錯過了在我的屬性旁邊提供括號()。 – Biki 2014-04-27 00:30:55