宝哥软件园

Angularjs双向绑定时字符串的转换成数字类型的问题

编辑:宝哥软件园 来源:互联网 时间:2021-09-04

问题:

!DOCTYPE html html towneta charset=' utf-8 '脚本src=' http :http://cdn。静态的。鲁诺布。com/libs/angular。js/1。4 .6/棱角分明。量滴js '/script/head dydiv ng-app=' myApp ' p ng-controller=' my controll '结果为span ng-bind=' '/span put type=' text ' ng-model=' first ' { first second } }/p/div脚本var app=angular。模块(' myApp ',[]);app.controller('myContrl ',函数($ scope){ $ scope。first=5;$ scope . second=10 });/脚本/正文/html显示结果为

但是,我要是输入50,想要结果为60

因为这个是字符串类型需要转换成数字类型

解决方法:

!DOCTYPE html html towneta charset=' utf-8 '脚本src=' http :http://cdn。静态的。鲁诺布。com/libs/angular。js/1。4 .6/棱角分明。量滴js '/script/head dydiv ng-app=' myApp ' p ng-controller=' my controll '结果为span ng-bind=' '/span put type=' text ' ng-model=' first ' { first * 1 second * 1 } }/p/div脚本var app=angular。模块(' myApp ',[]);app.controller('myContrl ',函数($ scope){ $ scope。first=5;$ scope . second=10 });/脚本/正文/html显示即可正常即是在{ {第一个*1秒*1}}显示的时候,转换了一下

或者,启用事件监听

!DOCTYPE html html towneta charset=' utf-8 '脚本src=' http :http://cdn。静态的。鲁诺布。com/libs/angular。js/1。4 .6/棱角分明。量滴js '/script/head dydiv ng-app=' myApp ' p ng-controller=' my controll '结果为span ng-bind=' '/span put type=' text ' ng-model=' first ' { total } }/p/div脚本var app=angular。模块(' myApp ',[]);app.controller('myContrl ',函数($ scope){ $ scope。first=5;$ scope.second=10$ scope。total=parse int($ scope)。首先)解析int($ scope)。第二);$scope .$ watch(function(){ return $ scope。首先;},函数(newValue,oldValue){ if(newValue!=旧值){ $ scope。total=parse int($ scope)。首先)解析int($ scope)。第二);} });});/脚本/正文/html

也能输出正确结果

以上所述是小编给大家介绍的角度射流研究…双向绑定时字符串的转换成数字类型的问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

更多资讯
游戏推荐
更多+