Two-way binding with range and number input in AngularJS

前端 未结 3 509
梦毁少年i
梦毁少年i 2021-02-07 06:39

I\'m just starting to play around with AngularJS and trying to understand the binding technique. For starters, I tried to make a simple conversion calculator (dozens to pieces,

3条回答
  •  轮回少年
    2021-02-07 07:21

    The problem here is that the input type="range" works with Strings and not with Numbers (while input type="number" only works with Numbers).

    http://www.w3.org/wiki/HTML/Elements/input/range

    The range state represents a control for setting the element's value to a string representing a number.

    If you add val = parseInt(val) as your first instruction on the qty setter it should work:

    this.__defineSetter__("qty", function (val) {        
        val = parseInt(val);
        qty = val;
        dozens = val / 12;
    });
    

    jsfiddle: http://jsfiddle.net/bmleite/2Pk3M/2/

提交回复
热议问题