简体   繁体   中英

ko observable not showing value

I have a ko observable variable declared within a view model

        selectedResource: {
            func: ko.observable
        },

This variable is being accessed within the javascript. This code is working fine and displays the variable values within the brower log

loadResourceMethodsAction: function(resource){
            console.log("Load Methods Action");
            this.bindings.selectedResource(resource);
            console.log(this.bindings.selectedResource().displayName());
            console.log(this.bindings.selectedResource().description());
            console.log(this.bindings.selectedResource().relativeUri());
        }

However when I try to read the same variable my DUST template. it fails to read it.

<p>TODO Methods</p>
<p>Selected Resource:</p>
<p>Display Name:<span data-bind="text: selectedResource.displayName"></span></p>
<p>Description :<span data-bind="text: selectedResource.description"></span></p>
<p>Relative URI:<span data-bind="text: selectedResource.relativeUri"></span></p>

I tried attaching () to both selectedResource and displayName but it still doesn't work.

I think you forgot to call it.

func: ko.observable

sets func to the actual ko.observable function; it doesn't call the function to actually create a knockout observable property.

Just change it to

func: ko.observable()

or

func: ko.observable(<default value>)

That said, are you sure you don't want the following?

selectedResource: ko.observable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM