javascript - 使用当前日期自动填充文本框

标签 javascript jquery angularjs html visual-studio

我在 aspx 页面中使用 AngularJS、HTML5 来创建访客签到信息:-

   <div style="border: dotted 1px grey; padding: 20px 0 20px 0; width: 40%;">
                <div class="row" style="margin-left: 30px">
                    <div style="display: inline-block;">
                        Visitor: 
                    </div>
                    <div style="display: inline-block; margin-left: 28px; width: 383px;">
                        <input type="text" data-ng-model="currentUser.Visitor">
                    </div>
                </div>
                <div style="margin-top: 20px"></div>
                <div class="row" style="margin-left: 30px">
                    <div style="display: inline-block;">
                        Title: 
                    </div>
                    <div style="display: inline-block; margin-left: 40px;">
                        <input type="text" data-ng-model="currentUser.Title">
                    </div>
                </div>
                <div style="margin-top: 20px"></div>
                <div class="row" style="margin-left: 30px">
                    <div style="display: inline-block;">
                        Visitee: 
                    </div>
                    <div style="display: inline-block; margin-left: 24px;">
                        <input type="text" data-ng-model="currentUser.Visitee">
                    </div>
                </div>
                <div style="margin-top: 20px"></div>
                <div class="row" style="margin-left: 30px">
                    <div style="display: inline-block;">
                        Arrival:
                    </div>
                    <div style="display: inline-block; margin-left: 25px;">
                        <input type="text" data-ng-model="currentUser.Arrival">
                    </div>
                </div>
                <div style="margin-top: 20px"></div>
                <div class="row" style="margin-left: 30px">
                    <div style="display: inline-block;">
                        Departure:
                    </div>
                    <div style="display: inline-block; margin-left: 0px;">
                        <input type="text" data-ng-model="currentUser.Departure">
                    </div>
                </div>
            </div>

            <div>
                <div style="margin: 2% 0 0 8%; display: inline-block">
                    <button data-ng-click="addNew(currentUser)" class="btn btn-primary" type="button">Add New Check-in</button>
                </div>

JSFiddle

我希望自动填充到达字段...所以在我的 Main.JS 中,我想说:-

var now = new Date();
$scope.currentUser.Arrival = now.format("dd/MM/yyyy, hh:mm tt");

now.format() ...不是原生js...但该函数按原样工作,只是不填充到文本框中。

但这不起作用...它禁用了我的 Angular 网格和之前输入的所有数据..Main.JS 的开头如下:-

 var app = angular.module('myApp', ['ngTouch', 'ui.grid']);
 app.controller('UserController', ['$scope', function ($scope) {
 var now = new Date();
 $scope.currentUser.Arrival = now.format("dd/MM/yyyy, hh:mm tt");
 $scope.gridOptions = {
    data: 'userList',
    columnDefs: [{ field: 'Visitor', displayName: 'Visitor' },
        { field: 'Title', displayName: 'Type of Visit' },
        { field: 'Visitee', displayName: 'Visitee' },
        {field: 'Arrival', displayName: 'Arrival Time'},
        {field: 'Departure', displayName: 'Departure Time'}]
  };... etc etc... fucntionalities....

最佳答案

当我在 JSFiddle 中运行该代码时,出现 now.format 不是函数的错误。

有什么原因不能使用 Angular $filter 吗?将其传递到您的 Controller 中:

app.controller('UserController', ['$scope', '$filter', function ($scope,$filter) {

然后指定日期:

$scope.currentUser.Arrival = $filter('date')(new Date(),'dd/MM/yyyy, hh:mm a');

另外,另一个注意事项是,根据您提供的信息,尚未为 $scope 定义 currentUser,因此不会分配 Arrival 属性。如果您以其他方式定义了 $scope.currentUser,那么就可以了。否则,您可能希望它看起来像:

$scope.currentUser = {};
$scope.currentUser.Arrival = $filter('date')(new Date(),'dd/MM/yyyy, hh:mm a');

关于javascript - 使用当前日期自动填充文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092169/

相关文章:

AngularJS:使用不同的 ng-app 指令在不同页面的 Controller 之间传递数据

javascript - Mongoose验证错误: first: Path `first` is required.,最后: Path `last` is required.“

javascript,正则表达式解析大括号中的字符串内容

javascript - JSON.parse 应该是递归的吗?

javascript - 如何在 JQuery 中使用 ajax() 函数?

javascript - 从 $resource 自动完成 Angularjs

javascript - ng-重复 : How to set property multiple in select

javascript - Footables 列排序会重复表中的行,为什么?

javascript - 将测试文件放在与源文件相同的文件夹中或单独放在 test/下的优缺点

javascript - 使用 AJAX 和 PHP 永久覆盖简单的点击计数