How to bind, display and use a value (that is taken from the slide bar) in an input text field?


#1

I am trying to modify a loan calculator example using Ionic Framework. What I am trying to do is to bind the value of the slide bar and the value that is in the input field. When I slide the slide bar let’s say to the value of 10, I want the text field input to display a value of 10 as well. And when I increase or decrease the number using the up and down arrows I want the value in the text field to not jump back to 0 again, but to start from the value that is the same with the slide bar. I have tried using ng-bind, ng-controller, ng-model and ng-change and only using placeholder at the moment but I know I shouldn’t use placeholder.

Below are the codes so far:

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);
    };    

});

So how can I bind the slider value to the input field so that when I change the slider value, the input field value change together as well and vice versa?