简体   繁体   English

AngularJS指令中的jQuery-ui datepicker

[英]jQuery-ui datepicker in angularjs directive

I have table with input fields with dates. 我有带有日期输入字段的表。 I need to change them by datepicker. 我需要按日期选择器更改它们。 With jquery-ui it works fine but I need to do this on angularjs.This is what I have right now, but it's not working for me. 使用jquery-ui可以正常工作,但我需要在angularjs上执行此操作,这是我现在拥有的功能,但对我不起作用。 Controller: 控制器:

.controller('TimesheetCtrl', ['$scope', 'restService',
     function($scope,  restService) {
         $scope.bindModel = function(data) {
             $scope.model = data;
         };
     restService.getTicketsInProgressList($scope.bindModel);
     }
]);

Directive: 指示:

.directive('datepicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            $(function () {
                element.datepicker({
                    dateFormat: 'dd/mm/yy',
                    onSelect: function (date) {
                        scope.$apply(function () {
                            ngModelCtrl.$setViewValue(date);
                        });
                    }
                });
            });
        }
    };
});

HTML: HTML:

<tbody ng-repeat="item in model | orderBy: '-TaskDate'">
    <tr class="timesheet-day-block">
        <td colspan="5"> <span class="timesheet-date">{{item.TaskDate | date: MM/dd/yyyy}}</span>

        </td>
    </tr>
    <tr class="timesheet-table-rows" ng-repeat="list in item.TimesheetList">
        <td>{{list.ProjectName}}</td>
        <td>
            <!-- HERE IS THE PROBLEM -->
            <input type="text" placeholder="dd/mm/yyyy" ng-model="list.TaskDate | date: 'dd/MM/yyyy'" datepicker/>
        </td>
        <td> <a ng-href="#/tickets/ticket/{{ticket.Id}}">{{list.Task}}</a>

        </td>
        <td>
            <input type="text" value="{{list.TimeWorked}}" placeholder="Elapsed time" style="width: 98%" />
        </td>
        <td>
            <input type="text" value="{{list.Note}}" placeholder="Note" style="width: 98%" />
        </td>
    </tr>
</tbody>

You can check with the below link. 您可以使用以下链接进行检查。

Fiddle 小提琴

    app = angular.module 'myapp', ['ng-bootstrap-datepicker']

AppCtrl = ($scope)->
  $scope.datepickerOptions =
    format: 'yyyy-mm-dd'
    language: 'fr'
    autoclose: true
    weekStart: 0

  $scope.date = '2000-03-12'

app.controller 'AppCtrl', AppCtrl    
angular.bootstrap document, ['myapp']

Inject $timeout in your directive and use it like this. 在指令中注入$timeout并像这样使用它。

.directive('datepicker', function ($timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            $timeout(function () {
                $(function () {
                    element.datepicker({
                        dateFormat: 'dd/mm/yy',
                        onSelect: function (date) {
                            scope.$apply(function () {
                                ngModelCtrl.$setViewValue(date);
                            });
                        }
                    });
                });
            });
        };
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM