我想呼籲ATTR src,卻沒有一個功能。這是我嘗試過的。動態調用上孩子的函數在knockout.js ATTR SRC
function FavoriteViewModel() {
var self = this
self.FavoriteProfiles = ko.observableArray([])
self.getRating = function(Rating){
//here i want conditions and concat image path
return does nothing here
}
self.LoadData = function(){
//run ajax and put its result in self.FavoriteProfiles
self.FavoriteProfiles(Result)
}
self.LoadData()
}
當我運行AJAX這帶來了這樣的結果。結果是多,我只是張貼單個對象瞭解
ProfileId 20
Age 21
Gender "F"
Rating 4
和IM這樣
<div id="favorite-related-profiles" data-bind="foreach:FavoriteProfiles">
<article class="prfl_box">
<p>
<span id="favorite-related-age" data-bind="text:Age"></span>
<span id="favorite-related-gender" data-bind="text:Gender"></span>
<br>
<img id="favorite-related-rating" class="pro_rating" src="" data-bind="attr:{src:Rating}">
</p>
</article>
</div>
數據綁定當我嘗試這種結合這樣
<img id="favorite-related-rating" class="pro_rating" src="" data-bind="attr:{src:$root.getRating.bind($data,Rating)}">
我在SRC得到這個
src="function() { [native code] }"
如何動態生成src屬性。
注意我需要顯示的圖像。圖像被命名爲4rating.png,5rating.png,爲Default.png。 我要檢查,如果評級是在src屬性4 assing 4rating。我怎樣才能做到這一點。
退房計算觀察到http://knockoutjs.com/documentation/computedObservables.html – Anders