Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout image src binding issue

Tags:

knockout.js

I have save store image name in database and image file in local folder I have used to bind the image

<img width="16px" height="16px" data-bind="attr:{src: PhotoName}"  />

in html it's show

<img src="http://sitename.com/Controller/action/imagename.extension"/>

but I need

<img src="http://sitename.com/imagefolder/imagename.extension"/>

any idea how can i fix this?? Thanks in advance.

like image 556
Ronjon Avatar asked Jun 02 '13 10:06

Ronjon


People also ask

What is binding in Knockout?

Essentially a binding or a data binding is a way to link your ViewModels to your Views(templates) and vice versa. KnockoutJS uses two-way data binding, which means changes to your ViewModel influence the View and changes to your View can influence the ViewModel.

What is $data in Knockout?

The $data variable is a built-in variable used to refer to the current object being bound. In the example this is the one of the elements in the viewModel.

How do you activate a KnockoutJS model?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.


1 Answers

Your issue has nothing to with kncokout.js. If your PhotoName only contains the imagename.extension you need to build your image path by hand in order to display the images correctly.

So you need to create the correct path either in the binding directly:

<img data-bind="attr:{ src: '/imagefolder/' + PhotoName }" />

Note if your PhotoName is a ko.observable then you need to write src: '/imagefolder/' + PhotoName().

Or move this logic inside your viewmodel e.g. creating a computed property which does the link building or when you create your viewmodel assign the correct url to PhotoName etc.

like image 199
nemesv Avatar answered Sep 19 '22 17:09

nemesv