最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout JS html binding returning weird code instead of html string - Stack Overflow

programmeradmin1浏览0评论
function tournamentViewModel(){
    var self= this;
    self.name = ko.observable();
    self.districts = ko.observableArray([new district('Provo',1),new district('Salt Lake City',2),new district('St. George',3)]);
    self.district = ko.observableArray();
    self.regions = ko.observableArray([new region('Utah',1),new region('Idaho',2)]);
    self.region = ko.observableArray();
    self.location = ko.observable();
    self.date = ko.observable();
    self.startTime = ko.observable();
    self.image = ko.observable();
    self.flyer = koputed(function(){return '<h1>'+self.name+'</h1>'+self.image},self);
    self.clearImage = function(){
        self.image(''); 
    }
    self.tournamentID = koputed(function(){return 't_'+self.district+'_'+self.region+'_'+self.date}, self);
};

The above knockout.js view model seems to be fine except for when I want to bind something to the puted observable flyer. Instead, all I see is the following text:

<h1>function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}</h1>function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}

I don't know what's going on here. Below is the binding I'm applying it to. I've tried both html and text bindings.

<span data-bind="text: flyer"></span>

BTW the puted observable tournamentID works great and the syntax seems identical. I think the problem occurs when I use self.name in the puted observable. Any ideas?

function tournamentViewModel(){
    var self= this;
    self.name = ko.observable();
    self.districts = ko.observableArray([new district('Provo',1),new district('Salt Lake City',2),new district('St. George',3)]);
    self.district = ko.observableArray();
    self.regions = ko.observableArray([new region('Utah',1),new region('Idaho',2)]);
    self.region = ko.observableArray();
    self.location = ko.observable();
    self.date = ko.observable();
    self.startTime = ko.observable();
    self.image = ko.observable();
    self.flyer = ko.puted(function(){return '<h1>'+self.name+'</h1>'+self.image},self);
    self.clearImage = function(){
        self.image(''); 
    }
    self.tournamentID = ko.puted(function(){return 't_'+self.district+'_'+self.region+'_'+self.date}, self);
};

The above knockout.js view model seems to be fine except for when I want to bind something to the puted observable flyer. Instead, all I see is the following text:

<h1>function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}</h1>function c(){if(0<arguments.length){if(!c.equalityComparer||!c.equalityComparer(d,arguments[0]))c.I(),d=arguments[0],c.H();return this}a.U.La(c);return d}

I don't know what's going on here. Below is the binding I'm applying it to. I've tried both html and text bindings.

<span data-bind="text: flyer"></span>

BTW the puted observable tournamentID works great and the syntax seems identical. I think the problem occurs when I use self.name in the puted observable. Any ideas?

Share Improve this question edited Nov 28, 2016 at 18:06 Vemonus 8661 gold badge16 silver badges28 bronze badges asked Jul 10, 2012 at 5:44 Frank BFrank B 6581 gold badge9 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Think about it. What do you get? You get the function definition. Because you passed function to your puted. And you need to pass values. You should use:

self.flyer = ko.puted(function(){
    return '<h1>'+self.name()+'</h1>'+self.image();
});

since both name and image are observables (from JavaScript point of view: functions).

I'm not sure why tournamentID is working for you. It shouldn't.

BTW If you are using var self = this;, then you can omit the second argument of puted.

try this

<span data-bind="text: flyer()"></span>
发布评论

评论列表(0)

  1. 暂无评论