如何在Angular JS中设置日期输入的值?

时间:2019-03-18 09:19:28

标签: angularjs

我创建了一个angular js应用程序,该应用程序使用保存在cookie中的客户ID从数据库中获取客户数据,并使用$scope函数在initialize()中设置获取的数据。但是问题在于不能在日期类型的input中设置客户的生日。我收到错误消息,提示类似错误:[ngModel:datefmt]。 如何在表单输入中设置获取日期?

以防万一,如果您需要知道,我在MySQL语句中使用DATE_FORMAT('birth_date','%M%d,%Y')作为birth_date来获取日期。

下面给出了代码。

Account.php

<?php $customer = (new Customer())->fetch_data($_COOKIE['customer-id']); ?>
<div id="account-settings" ng-app="settings"  ng-controller="personal-data" 
    ng-init="initialize()">

    <div id="ausweis-daten" class="header">
    <h4>Identity Document</h4>
    </div>
    <div class="body">
        <label>First Name: </label>
        <input type="text" class="text-box" ng-model="firstname" name="first-name" placeholder="First Name">

        <label>Last Name: </label>
        <input type="text" class="text-box" ng-model="lastname" name="last-name" placeholder="Last Name">
        <br/>
        <label>Date of Birth: </label>
        <input type="date" class="text-box" name="birth_date" ng-model="birth_date" placeholder="Date of Birth">
        <br/>
        <label>Place of Birth: </label>
        <input type="text" class="text-box" name="birth_place" ng-model="birth_place" placeholder="Place of Birth"><br/>
        <input type="submit" class="save-button" name="submit" ng-click="save_id()" value="Done">
    </div>
</div>

Angular js文件:

    var app = angular.module('settings',[]);

    app.controller('personal-data', function($scope, $http){


        $scope.initialize = function(){
            $scope.firstname = "<?php echo $customer['fname'] ?>";
            $scope.lastname = "<?php echo $customer['lname']; ?>";
            $scope.birth_date = "<?php echo $customer['birth_date']; ?>";
            $scope.birth_place = "<?php echo $customer['birth_place']; ?>";
        }

        $scope.save_id = function(){

            var send_obj = {
                firstname: $scope.firstname,
                lastname: $scope.lastname,
                birth_date:$scope.birth_date,
                birth_place:$scope.birth_place
            };

            $http.post("functions/_customer_action.php", send_obj).then(function(success){
                alert(success.data);
            });
        }       
    });
</script>

1 个答案:

答案 0 :(得分:-1)

必须以有效的ISO-8601日期格式yyyy-MM-dd输入文本。
例如:2009-01-06。使用DATE_FORMAT('birth_date', '%Y-%M-%d')