Ionic Framework / AngularJS - 如何绑定,显示和使用输入文本字段中的值(来自滑动条)?

时间:2015-09-22 03:07:09

标签: angularjs ionic-framework ionic

我正在使用Ionic Framework制作贷款计算器。我想要做的是绑定滑动条的值和输入字段中的值。当我滑动滑动条时,让我们说10的值,我希望文本字段输入也显示值10。当我使用向上和向下箭头增加或减少数字时,我希望文本字段中的值不再跳回0,而是从与滑动条相同的值开始。我尝试过使用ng-bind,ng-controller,ng-model和ng-change,目前只使用占位符,但我知道我不应该使用占位符。

以下是我的代码:

的index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Payment Calculator</h1>
      </ion-header-bar>

      <ion-content>

      <!--start-->

        <form name="MyForm">
            <div class="range range-assertive" ng-controller="ListCtrl" >
                <div class="list card">
                    <label class="item item-input">
                        <span class="input-label">Loan Amount</span>
                            <input type="number" name="MyTextField" id="loan.amount" value="{{user.value}}"  ng-model="user.value" ng-change="myChange(user.value)" ng-bind="user.value" placeholder=
                            "{{user.value}}">  
                    </label>
                    <i class="icon ion-social-euro-outline"></i>
                        <input type="range" min="{{user.min}}" max="{{user.max}}" value="{{user.value}}" step="1" ng-model="user.value" ng-change="myChange(user.value)" ng-bind=
                        "user.value">
                        <span ng-bind="user.value"></span>  
                        /{{user.max}}
                    <i class="icon ion-social-euro"></i> 

                    <label class="item item-input">
                        <span class="input-label">Periods</span>
                            <input type="number" id="periods" value= "{{periods.value}}" ng-model="periods.value" ng-change="myChange(periods.value)" placeholder="{{periods.value}}">
                    </label>
                    <i class="icon ion-social-euro-outline"></i>
                        <input type="range" min="{{periods.min}}" max="{{periods.max}}" value="{{periods.value}}" step="1" ng-model="periods.value" ng-change=
                        "myChange(periods.value)">
                        {{periods.value}}
                        /{{periods.max}}
                    <i class="icon ion-social-euro"></i>  

                    <label class="item item-input">
                        <span class="input-label">Rate</span>
                        <input type="number" id="rate" value= "{{rate.value}}" ng-model="rate.value" ng-change="myChange(rate.value)" placeholder="{{rate.value}}">
                    </label>
                    <i class="icon ion-social-euro-outline"></i>
                        <input type="range" min="{{rate.min}}" max="{{rate.max}}" value="{{rate.value}}" step="1" ng-model="rate.value" ng-change="myChange(rate.value)">
                        {{rate.value}}
                        /{{rate.max}}
                    <i class="icon ion-social-euro"></i> 
                </div>
            </div>

            <button onclick="pmtCalc()" class="button" style="margin: 1em 1em 1em 1em">Calculate</button><br>  

            <div class="list">
                <label class="item item-input">
                    <span class="input-label">Answer</span>
                    <p type="number" id="ANSWER"></p>
                </label>
            </div>

            <!--<p id="POWER"></p>-->

            <script>
                function pmtCalc() {
                /*http://www.financeformulas.net/Loan_Payment_Formula.html*/
                L = parseFloat(document.getElementById("loan.amount").value);
                P = parseFloat(document.getElementById("periods").value);
                R = parseFloat(document.getElementById("rate").value);

                N = L * R/12/100;
                D = 1 - Math.pow(1+(R/12/100),-P);

                ANS = N/D
                document.getElementById("ANSWER").innerHTML = ANS;
                /*document.getElementById("POWER").innerHTML = D;*/
                }
            </script>
        </form>

      <!--END!-->

      </ion-content>
    </ion-pane>
  </body>
</html>

app.js:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'


var app = angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

app.controller('ListCtrl', function($scope) {

    $scope.user= {
        min:0,
        max:200000,
        value:0
    }

    $scope.myChange=function(val){
        console.log("on-change",$scope.user);
        console.log("on-change",val);
    };

    $scope.periods= {
        min:0,
        max:120,
        value:0
    }

    $scope.myChange2=function(val){
        console.log("on-change",$scope.periods);
        console.log("on-change",val);
    };

    $scope.rate= {
        min:0,
        max:20,
        value:0
    }

    $scope.myChange3=function(val){
        console.log("on-change",$scope.rate);
        console.log("on-change",val);
    };    

});

那么如何将滑块值绑定到输入字段,这样当我更改滑块值时,输入字段值也会一起变化,反之亦然?

2 个答案:

答案 0 :(得分:1)

问题是由于范围输入 返回字符串值而不是数字作为数字输入。 出于这个原因,我通常使用这样的指令:

app.directive('input', function () {
    return {
        restrict: 'E',
        require: 'ngModel',
        link: function (scope, elem, attrs, ctrl) {
            if (attrs.type === 'range') {
                ctrl.$parsers.push(function (value) {
                    return isFinite(parseFloat(value)) ? parseFloat(value) : ctrl.$modelValue;
                });
            }
        }
    };
});

我还重构了您的代码,删除了输入标记上的所有无用value属性,以及视图中的“非法”js,并使用更优雅的$watchGroup实时计算“答案”:

控制器:

  $scope.user = {
    min: 0,
    max: 200000,
    value: 0
  };

  $scope.periods = {
    min: 0,
    max: 120,
    value: 0
  };  

  $scope.rate = {
    min: 0,
    max: 20,
    value: 0
  };  
  $scope.answer = 0;

  $scope.$watchGroup([
      function(){ return $scope.user.value}, 
      function(){ return $scope.periods.value},
      function(){ return $scope.rate.value}
    ], function(newValues, oldValues){

      var l = parseFloat(newValues[0]);
      var p = parseFloat(newValues[1]);
      var r = parseFloat(newValues[2]);   
      var n = l * r / 12 / 100;
      var d = 1 - Math.pow(1 + ( r / 12 / 100 ), -p);
      var answer = n / d;

      $scope.answer = answer || 0;

  });

HTML:

<div class="list card">
  <label class="item item-input">
    <span class="input-label">Loan Amount {{user.value}}</span>
    <input type="number" name="MyTextField" id="loan.amount" ng-model="user.value">
  </label>
  <i class="icon ion-social-euro-outline"></i>
  <input type="range" min="{{user.min}}" max="{{user.max}}" step="1" ng-model="user.value"> {{user.value}}/{{user.max}}
  <i class="icon ion-social-euro"></i>

  <label class="item item-input">
    <span class="input-label">Periods</span>
    <input type="number" id="periods" ng-model="periods.value" placeholder="{{periods.value}}">
  </label>
  <i class="icon ion-social-euro-outline"></i>
  <input type="range" min="{{periods.min}}" max="{{periods.max}}" step="1" ng-model="periods.value"> {{periods.value}} /{{periods.max}}
  <i class="icon ion-social-euro"></i>

  <label class="item item-input">
    <span class="input-label">Rate</span>
    <input type="number" id="rate" ng-model="rate.value" placeholder="{{rate.value}}">
  </label>
  <i class="icon ion-social-euro-outline"></i>
  <input type="range" min="{{rate.min}}" max="{{rate.max}}" step="1" ng-model="rate.value"> {{rate.value}} /{{rate.max}}
  <i class="icon ion-social-euro"></i>
</div>
<div class="item">Answer: {{answer}}</div>

Take a look to the demo codepen

享受!

答案 1 :(得分:0)

这是我使用的:

          我的范围         
proc contents data = sashelp.cars out = mycontents;
run;

%macro fun(var);
%global obs;
proc sql noprint;
        select count(distinct(&var.))
        into :obs
        from sashelp.cars;
quit;
%mend;

data work.test;
set mycontents;
if name ne "Type" then do;
      rc = dosubl(cats('%fun(',name,')'));
      new = symgetn('obs');
end;
else new = 5;
run;

然后我的控制器:

    <ion-item class="range range-positive" id="range1">
      <label>0</label>
      <input type="range" value="{{slider.rangeValue}}" ng-model="slider.rangeValue"  min="0" max="100" step="1" name="myrange">
      <label>80</label>
    </ion-item>
    <div class="item item-divider">
      Value: {{slider.rangeValue}}
    </div>