如何更新"输入"实际可见"价值"在页面上使用AngularJS

时间:2015-09-25 12:29:55

标签: javascript html angularjs

我在设置以编程方式填充的某些输入字段的视图值时遇到问题:

  1. 当它为空时,脚本会添加" 0&#34 ;;
  2. 如果数字> 360,脚本设置为" 360"。
  3. <p>
        <span style="color: #81a0ff; font-size: 3em; font-weight: bold;">
            &alpha; = {{angle1}}&deg;
        </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);
    }
    

    我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案:

   http://jsfiddle.net/MTfRD/2207/  

结果:脚本根据minmax函数校正数字(最后一个必须在控制器中),angularjs显示输入字段的可见值。
感谢大家的帮助,尤其感谢Asok提示。
HTML

<div ng-app="DispAngleApp"  ng-controller="AppCtrl as app">
<p><span class="alfa">&alpha; = {{angle1}}&deg;</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">&beta; = {{angle2}}&deg;</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];}