2014-01-14 106 views
0
$("#ServiceID" + viewModel.ServiceID()).attr("src", viewModel.smallImageUrl()); 

#ServiceID(我的圖片ID)和viewmodel.ServiceID(是我的唯一ID)和viewmodel.smallImageUrl()是在CDN不能改變我的形象物業

我的圖像URL更改我的形象基礎上我唯一的ID。但它不適合我。我不知道jQuery中的確切語法。

+1

檢查什麼的'「#ServiceID」 + viewModel.ServiceID()'的結果,並確認是否有與任何HTML元素ID。 –

+1

有幾件事要檢查。 viewModel.ServiceID()'和'viewModel.smallImageUrl()'實際上有值嗎? 'console.log($('#ServiceID「+ viewModel.ServiceID()));'return anything? –

+0

請提供一些小提琴http://jsfiddle.net/ –

回答

0

正如@Anders已經指出的那樣,Knockout促進了MVVM方法。就是說,這意味着從演示文稿中分離數據。這意味着當你在viewmodel中做jQuery時,你做錯了(對不起,如果這聽起來很刺耳)。

推薦的方法如下:在HTML中查找圖像並對其應用綁定,以便將src設置爲viewmodel的屬性。因此,這將是這樣的:

<img id="ServiceID3" data-bind="attr: { src: $root.smallImageUrl }" /> // $root points to the viewModel you've bound using ko.applyBindings 

某處在你的JavaScript:

ko.applyBindings(viewModel); 

如果這也不行,一個簡單的方法,如果您的視圖模型的觀察量保持您所期望的值進行調試他們舉行,是一個div添加到您的網頁下面的結合:

<div data-bind="text: ko.toJSON($root)"></div> // This will output the contents of your viewmodel as a string