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
そして、このようなデータをバインドしています
<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 であるかどうかを確認したい。これどうやってするの。