我在设置以编程方式填充的某些输入字段的视图值时遇到问题:
<p>
<span style="color: #81a0ff; font-size: 3em; font-weight: bold;">
α = {{angle1}}°
</span>
</p>
<input type="text" id="ang_1" maxlength="3"
onfocus = "this.value = parseInt(value) || ''"
onblur = "this.value = parseInt(value) || '0'"
onkeyup="this.value = minmax(this.value, 0, 360) || 0"
ng-model="angle1"
ng-init="angle1='0'" >
function minmax(value, min, max)
{
if (parseInt(value)== 0 || parseInt(value)== 00 || parseInt(value)== 000 )
return 0;
if(parseInt(value) < min)
return 0;
else if(parseInt(value) > max)
return 360;
else return value = parseInt(value);
}
我该如何解决这个问题?
答案 0 :(得分:0)
这是我的问题的解决方案:
http://jsfiddle.net/MTfRD/2207/
结果:脚本根据minmax函数校正数字(最后一个必须在控制器中),angularjs显示输入字段的可见值。
感谢大家的帮助,尤其感谢Asok提示。
HTML
<div ng-app="DispAngleApp" ng-controller="AppCtrl as app">
<p><span class="alfa">α = {{angle1}}°</span></p>
<input type = "text"
id = "ang_1"
maxlength = "3"
onfocus = "this.value = parseInt(value) || ''"
onblur = "this.value = parseInt(value) || '0'"
ng-change = "minmax(angle1, 0, 360) || 0"
ng-model = "angle1"
ng-init = "angle1='0'" ><br>
<p><span class = "beta">β = {{angle2}}°</span></p>
<input type = "text"
id = "ang_2"
maxlength = "3"
onfocus = "this.value = parseInt(value) || ''"
onblur = "this.value = parseInt(value) || '0'"
ng-change = "minmax2(angle2, 0, 360) || 0"
ng-model = "angle2"
ng-init = "angle2='0'"><br>
AngularJS:
var myApp = angular.module('myApp',[]); function AppCtrl($scope) {
$scope.minmax = function (value, min, max) {
if (value == '')
return $scope.angle1 = 0;
if (parseInt(value) == 0 || parseInt(value) == 00 || parseInt(value) == 000)
return $scope.angle1 = 0;
if (parseInt(value) < min)
return $scope.angle1 = 0;
else if (parseInt(value) > max)
return $scope.angle1 = 360;
else return $scope.angle1 = parseInt(value);
};
$scope.minmax2 = function (value, min, max) {
if (value == '')
return $scope.angle2 = 0;
if (parseInt(value) == 0 || parseInt(value) == 00 || parseInt(value) == 000)
return $scope.angle2 = 0;
if (parseInt(value) < min)
return $scope.angle2 = 0;
else if (parseInt(value) > max)
return $scope.angle2 = 360;
else return $scope.angle2 = parseInt(value);
};
$scope.change_val = function (value){
var va = value;
va = va.replace(/^\[\'|\'\]$/g,'').split("', '");
$scope.angle1 = va[0];
$scope.angle2 = va[1];
} } function change_val(el){
var ang_1 = document.getElementById("ang_1");
var ang_2 = document.getElementById("ang_2");
var v = el.value;
v = v.replace(/^\[\'|\'\]$/g,'').split("', '");
ang_1.value = v[0];
ang_2.value = v[1];}