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

javascript - Simple String Replace in Knockout - Stack Overflow

programmeradmin3浏览0评论

I keep getting the following error when trying to do a simple string replace with an observable.

str.replace is not a function

Here's a sample of what I mean. I got the following observable, which works fine:

this.price = ko.observable(data.price);
this.priceFloat = koputed(function() {
return parseFloat( Math.abs(viewModel.price()) ).toFixed(2);
}, this);

As to why I need a puted version and an observable version of the same number is another issue. Now I need to remove my mas, which is pretty basic. I simply did this in my console and it worked.

var str = "3,047";
var nw = str.replace(',', '');
console.log(nw);

But when I put it in the puted function, it returns the error.

this.price = ko.observable(data.price);
this.priceFloat = koputed(function() {
    var str = viewModel.price();
    var nw = str.replace(',', '');
    console.log(nw);
return parseFloat( Math.abs(viewModel.price()) ).toFixed(2);
}, this);

I tried the following as well but it didn't work, it still returns the same error.

ko.extenders.removeComma = function(target) {

  var result = koputed({
    read: function () { return target(); },
    write: function (v) { 
      target(v.replace(/\,/g, ''));
    }
  });
  return result;
};
this.price = ko.observable(data.price).extend({removeComma: ""});

Does anyone know why is this so? Any explanation would be appreciated.

I keep getting the following error when trying to do a simple string replace with an observable.

str.replace is not a function

Here's a sample of what I mean. I got the following observable, which works fine:

this.price = ko.observable(data.price);
this.priceFloat = ko.puted(function() {
return parseFloat( Math.abs(viewModel.price()) ).toFixed(2);
}, this);

As to why I need a puted version and an observable version of the same number is another issue. Now I need to remove my mas, which is pretty basic. I simply did this in my console and it worked.

var str = "3,047";
var nw = str.replace(',', '');
console.log(nw);

But when I put it in the puted function, it returns the error.

this.price = ko.observable(data.price);
this.priceFloat = ko.puted(function() {
    var str = viewModel.price();
    var nw = str.replace(',', '');
    console.log(nw);
return parseFloat( Math.abs(viewModel.price()) ).toFixed(2);
}, this);

I tried the following as well but it didn't work, it still returns the same error.

ko.extenders.removeComma = function(target) {

  var result = ko.puted({
    read: function () { return target(); },
    write: function (v) { 
      target(v.replace(/\,/g, ''));
    }
  });
  return result;
};
this.price = ko.observable(data.price).extend({removeComma: ""});

Does anyone know why is this so? Any explanation would be appreciated.

Share Improve this question asked Apr 30, 2013 at 19:13 Rachelle UyRachelle Uy 8581 gold badge10 silver badges15 bronze badges 4
  • Please log out the console.log(str); before calling replace on it, then you will see what viewModel.price() returns! – nemesv Commented Apr 30, 2013 at 19:18
  • 1 @Rachelle Uy Can you post the error message you receive? – Matthew Cox Commented Apr 30, 2013 at 19:39
  • @nemesv I know, then after the string replace function it doesn't work. :( – Rachelle Uy Commented Apr 30, 2013 at 19:44
  • @FeistyMango It's as simple as str.replace is not a function – Rachelle Uy Commented Apr 30, 2013 at 19:47
Add a ment  | 

2 Answers 2

Reset to default 2

Most likely it's because the observable is returning a value that isn't a string which would cause this error. See below for a way to handle that:

this.price = ko.observable(data.price);
this.priceFloat = ko.puted(function() {
    var str = viewModel.price() ? viewModel.price().toString() : '';
    var nw = str.replace(',', '');
    console.log(nw);
return parseFloat( Math.abs(viewModel.price()) ).toFixed(2);
}, this);

Edit:

http://jsfiddle/Qbc7Z/3/

In mented ko you can use like this...

<!-- ko text: address().telephone.toString().replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3") --><!-- /ko --><br/>
发布评论

评论列表(0)

  1. 暂无评论