javascript - Knockout Computed Observable with parameters -
javascript - Knockout Computed Observable with parameters -
is possible provide computed observable parameter?
for example, this:
var viewmodel = function(first, last) { this.firstname = ko.observable(first); this.lastname = ko.observable(last); var self = this; this.fullname = ko.computed(function(separator) { homecoming self.firstname() + ' ' + self.lastname(); }, this); };
and in html:
<div data-bind="text: fullname(' - ')"></div>
my actual utilize case far more complicated, i'm trying achieve, pass in value in html used part of computed function.
failing there way create ordinary function takes parameters behave (computed) observable?
you can create function, returns computed variable. can seek this.
var viewmodel = function(first, last) { this.firstname = ko.observable(first); this.lastname = ko.observable(last); var self = this; this.fullname = function(separator){ homecoming ko.computed(function () { homecoming self.firstname() + separator + self.lastname();}, this); }; }; <div data-bind="text: viewmodel.fullname('-')"></div>
javascript knockout.js
Comments
Post a Comment