控制器:
angular.module('myApp', [])
.controller('MainCtrl', function($scope) {
$scope.d = { v1: '100', v2: '20' };
});
模板:
<div ng-app="myApp">
<div ng-controller="MainCtrl">
1: <input type="text" ng-model="d.v1" />
2: <input type="text" ng-model="d.v2" />
<h4>{{d.v1}} < {{d.v2}} = {{d.v1 < d.v2}}</h4>
</div>
</div>
http://jsfiddle.net/GDfxd/292/
此示例错误地显示100小于20.任何建议如何使此比较数字?
答案 0 :(得分:1)
您的变量类型为“text”,您必须将其更改为“number”才能获得正确的结果。
答案 1 :(得分:0)
您需要的只是比较数字而不是字符串
以下是工作片段:
angular.module('myApp', [])
.controller('MainCtrl', function($scope) {
$scope.d = { v1: 100, v2: 20 };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MainCtrl">
1: <input type="text" ng-model="d.v1" />
2: <input type="text" ng-model="d.v2" />
<h4>{{d.v1}} < {{d.v2}} = {{d.v1 < d.v2}}</h4>
</div>
</div>
答案 2 :(得分:0)
试试这个,我刚刚添加了一个Eval函数,并将其他所有内容保持不变!
控制器:
angular.module('myApp', [])
.controller('MainCtrl', function($scope) {
$scope.d = { v1: '100', v2: '20' };
$scope.eval = function() {
return (parseInt($scope.d.v1, 10) < parseInt($scope.d.v2, 10));
}
});
模板:
<div ng-app="myApp">
<div ng-controller="MainCtrl">
1: <input type="text" ng-model="d.v1" />
2: <input type="text" ng-model="d.v2" />
<h4>{{d.v1}} < {{d.v2}} = {{eval()}}</h4>
</div>
</div>