Search code examples
javascripthtmlangularjsangularjs-scopeangular-ui-router

Input fields (type="number") loose their two-way 'BIND' property of angularjs once their content is changed.


My problem is pretty simple but elusive in nature. When you will load the index.php, (as localhost using xampp) you will be presented with a simple form. Now there are multiple elements on this form, and it's still a work in progress, so the possibility of multiple bugs is plausible. However, there's this one bug that's really annoying.

THE PROBLEM:

On changing the Due Date element, the content of the following input boxes changes due to the fact that they're bind with it. Now it won't matter how many times you change the due date, because every time the value in the fields will change accordingly, Thanks to angularjs.

The BUG creeps in when you change the value of an input field. Say originally it was 27 and then you changed it to , idk say 10.* NOW IF YOU CHANGE THE DUE DATE, THE ALTERED INPUT FIELD REMAINS THE SAME * I.E. WITH VALUE 10, WHEREAS I WANT IT TO CHANGE NEVERTHELESS.

Moreover, if one of you guys is the apotheosis of angularjs coders, and he got some tips for me, I would just like to say..... "I APPRECIATE THAT".

index.php

    <!-- addService.html -->
<?php
    $version = time();
?>
<!DOCTYPE html>
<html>
<head>

    <!-- CSS (load bootstrap and our custon css files) -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    <!-- JS (load angular, ui-router and our custom js file) -->
    <script src="http://code.angularjs.org/1.2.13/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="ctrl-add-service.js"></script>
    <script src="services.js"></script>
</head>
<body>
<div ng-app="mainApp" ng-controller="ctrl-add-service">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <h1 align="center">SERVICE FORM</h1>
            <form role="form" ng-submit="createService()"> 
                <div>
                    <label>Service Name</label>
                    <input type="text" class="form-control" placeholder="Enter service name here" ng-pattern="/^[a-zA-Z0-9_]*$/" required>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <label>Due Date</label>
                        <input type="date" class="form-control" ng-model='dueDate' ng-change="setFields()" required>
                    </div>
                    <div class="col-md-6">
                        <label>Task Date</label>
                        <input type="date" class="form-control" required>
                    </div>
                </div>
                <div style="margin-top: 20px;margin-bottom: 20px;" align="center">
                    <label>Period</label>
                    <label class="radio-inline"><input type="radio" ng-model="value" value='12' ng-change="setFields()">Annually</label>
                    <label class="radio-inline"><input type="radio" ng-model="value" value='6' ng-change="setFields()">Semi-Annually</label>
                    <label class="radio-inline"><input type="radio" ng-model="value" value='4' ng-change="setFields()">Quarterly</label>
                    <label class="radio-inline"><input type="radio" ng-model="value" value='1' ng-change="setFields()">Monthly</label>
                </div>
                <div align="center">
                    <div>
                        <div style="display:inline-block;"><label>Jan</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[0]' ng-required='!fields[0]'></div>
                        <div style="display:inline-block;"><label>Feb</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[1]' ng-required='!fields[1]'></div>
                        <div style="display:inline-block;"><label>Mar</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[2]' ng-required='!fields[2]'></div>
                        <div style="display:inline-block;"><label>Apr</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[3]' ng-required='!fields[3]'></div>
                    </div>

                        <div style="display:inline-block;"><label>May</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[4]' ng-required='!fields[4]'></div>
                        <div style="display:inline-block;"><label>Jun</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[5]' ng-required='!fields[5]'></div>
                        <div style="display:inline-block;"><label>Jul</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[6]' ng-required='!fields[6]'></div>
                        <div style="display:inline-block;"><label>Aug</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[7]' ng-required='!fields[7]'></div>

                    <div>
                        <div style="display:inline-block;"><label>Sep</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[8]' ng-required='!fields[8]'></div>
                        <div style="display:inline-block;"><label>Oct</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[9]' ng-required='!fields[9]'></div>
                        <div style="display:inline-block;"><label>Nov</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[10]' ng-required='!fields[10]'></div>
                        <div style="display:inline-block;"><label>Dec</label><input type="number" class="form-control" ng-value='date' ng-disabled='fields[11]' ng-required='!fields[11]'></div>
                    </div>
                </div>
                <div  align="center" style="margin-top: 20px;">
                    <button type="submit" class="btn btn-primary">Create</button>
                    <button type="reset" class="btn btn-danger">Reset</button>
                </div>
            </form>
        </div>
        <div class="col-md-2"></div>
    </div>
</div>
</body>
</html>

ctrl-add-service.js (controller)

    // ctrl-add-service.js          Controller for the add service option in the nav bar of the home screen.
var mainApp = angular.module("mainApp",[]); 

mainApp.controller('ctrl-add-service',function($scope, DueDateService){

    $scope.value ='1';
    $scope.setFields = function() {
        $scope.date = DueDateService.date($scope.dueDate);
        $scope.fields = DueDateService.fields( DueDateService.month($scope.dueDate), $scope.value);         // first parameter passes month in int, second parameter passes period value in int.
    };
}); 

services.js (services for the app)

    // services.js          services.js of the account direcotry of the project. It is used by the mainApp.     

//DueDateService

mainApp.service('DueDateService', function(){

    this.month = function(date) {
        var temp = new Date(date);
        month = temp.getMonth();
        console.log(month+1+" is the month");
        return (month+1);
    };

    this.date = function(date) {
        var temp = new Date(date);
        date = temp.getDate();
        console.log(date+" is the date");
        return (date);
    };


    this.fields = function(month,period) {
        var lap = parseInt(period);         // possible values of lap can be [12,6,4,1]
        var iteration = 12/lap;             // possible values of iteration can be [1,2,3,12]       
        var selectedFields = [true,true,true,true,true,true,true,true,true,true,true,true];     

        for (var i=1; i<=iteration; i++) {
            if(month>12) {
                month = month - 12;
            }
            selectedFields[month-1]= false;
            month = month + lap;
        }

        return selectedFields;
    };

});

Solution

  • I think you need to change the ng-value to ng-model and create an Array of your dates, like this:

    ng-model='dates[0]'
    ng-model='dates[1]'
    ng-model='dates[2]'
    ...
    

    And your controller would be like this:

    var date = DueDateService.date($scope.dueDate);
    $scope.dates = Array(12).fill(date);
    

    See at this plunker: https://plnkr.co/edit/p8O14Y80hCWyNmxnYxbF