大佬教程收集整理的这篇文章主要介绍了在AngularJS中双向绑定范围和数字输入,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
常见的JavaScript为破碎和工作的fiddles:
var myApp = angular.module('myApp',[]); myApp.controller('CalcCtrl',function ($scope) { var num = 0.0; $scope.qty = new Quantity(12); $scope.num = num; }); function Quantity(numOfPcs) { var qty = numOfPcs; var dozens = numOfPcs / 12; this.__defineGetter__("qty",function () { return qty; }); this.__definesetter__("qty",function (val) { qty = val; dozens = val / 12; }); this.__defineGetter__("dozens",function () { return dozens; }); this.__definesetter__("dozens",function (val) { dozens = val; qty = val * 12; }); }
HTML:
<div ng-controller="CalcCtrl"> <form> <label for="pcs">Pieces:</label> <input type="number" min="0" ng-model="qty.qty" size="20" id="pcs" /> <input type="range" min="0" max="100" ng-model="qty.qty" /> <br/> <label for="numOfDozens">Dozens</label> <input type="number" min="0" ng-model="qty.dozens" size="20" id="numOfDozens" /> </form> </div>
然而,将两个数字输入绑定到相同的模型属性似乎可以正常工作,如这个小提示:
HTML:
<div ng-controller="CalcCtrl"> <form> <label for="pcs">Pieces:</label> <input type="number" min="0" ng-model="qty.qty" size="20" id="pcs" /> <input type="number" min="0" max="100" ng-model="qty.qty" /> <br/> <label for="numOfDozens">Dozens</label> <input type="number" min="0" ng-model="qty.dozens" size="20" id="numOfDozens" /> </form> </div>
http://www.w3.org/wiki/HTML/Elements/input/range
如果您添加val = parseInt(val)作为您的第一个指令在数量设置器应该工作:
this.__definesetter__("qty",function (val) { val = parseInt(val); qty = val; dozens = val / 12; });
以上是大佬教程为你收集整理的在AngularJS中双向绑定范围和数字输入全部内容,希望文章能够帮你解决在AngularJS中双向绑定范围和数字输入所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。