javascript - 输入字段(类型 ="number")一旦其内容发生更改,就会丢失 angularjs 的双向 'BIND' 属性。

标签 javascript html angularjs angularjs-scope angular-ui-router

我的问题非常简单,但本质上却难以捉摸。当您加载index.php(使用xampp 作为本地主机)时,您将看到一个简单的表单。现在此表单上有多个元素,并且仍在进行中,因此存在多个错误的可能性是合理的。然而,有一个错误确实很烦人。

问题:

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

});

最佳答案

我认为您需要将 ng-value 更改为 ng-model 并创建日期的Array,如下所示:

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

你的 Controller 将是这样的:

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

看看这个笨蛋:https://plnkr.co/edit/p8O14Y80hCWyNmxnYxbF

关于javascript - 输入字段(类型 ="number")一旦其内容发生更改,就会丢失 angularjs 的双向 'BIND' 属性。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38396782/

相关文章:

javascript - 去除 iFrame 的 html 标签样式

javascript - 在保持可排序性的同时更改 JqueryUI 可排序元素的类

angularjs - 无法从 AngularJS 中的 Controller 设置日期选择器日期

javascript - 如何使用ajax仅加载wordpress帖子的内容

javascript - 如何使用 jsoup 发布 javascript 表单?

javascript - 如何将窗口对象从事件选项卡发送到扩展?

html - CSS 固定下拉菜单行为怪异

javascript - 类型错误 : Cannot read property 'open' of undefined (Phonegap and Ionic)

javascript - 在 Internet Explorer 中查看 localStorage - 无法获取名称/值对

javascript - 下拉菜单在CSS中折叠