javascript - Using string based variables in knockouts controlflow templates -


i'm using knockout's containerless controlflow templates:

<div data-bind="foreach: mydata" style="margin-top: 10px;">         <div>             <a href="#" data-bind="attr: {href: url}" target="_blank">                 <img src="http://www.google.com/s2/favicons?domain={{url}}" />             </a>         </div> </div> 

getting url href working, want call url value again in image src. want keep beginning part of image source , add url end. how 1 using knockout template?

for simple task don't need additional templating because ko lets write arbitrary expression in bindings string concatenation.

so can build url right in attr binding:

<div data-bind="foreach: mydata" style="margin-top: 10px;">     <div>         <a href="#" data-bind="attr: {href: url}" target="_blank">             <img data-bind="attr: {                   src: 'http://www.google.com/s2/favicons?domain=' + url()}" />         </a>    </div> </div> 

note: need write url() if url property ko.observable value inside binding expression.

however more proper solution calculate image url inside view model , have imageurl property bind <img data-bind="attr: { src: imageurl }"/>


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -