简体   繁体   English

AngularJS UI Bootstrap DatePicker无法正常运行

[英]AngularJS UI Bootstrap DatePicker Not Functioning Correctly

I have a directive defined as follows: 我有一个定义如下的指令:

Template 模板

<div class="date-picker">
    <div class="row">
        <div class="col-md-6">
            <label for="{{datePickerId}}">{{labelText}}</label>
            <p class="input-group">
                <input type="text"
                       id="{{datePickerId}}"
                       class="form-control"
                       datepicker-popup="{{format}}"
                       ng-model="dt"
                       is-open="opened"
                       datepicker-options="dateOptions"
                       disabled="disabled"
                       ng-required="true"
                       close-text="Close" />
                <span class="input-group-btn">
                    <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
                </span>
            </p>
        </div>
    </div>
</div>

Directive 指示

//TypeScript

module MyApp.Directives {
    /** Scope for Messaging directive. */
    export interface IMaDatePickerScope extends ng.IScope {
        dt: Date;
        today(): void;
        clear(): void;
        disabled(date, mode): boolean;
        open($event): void;
        opened: boolean;
        dateOptions;
        formats: string[];
        format: string;
    }


    export class MaDatePicker implements ng.IDirective {
        restrict = "E";
        templateUrl = TEMPLATES + 'ma-date-picker.html';
        replace = true;
        transclude = true;
        scope = {
            labelText: '@',
            datePickerId: '='
        }
        link = (scope: IMaDatePickerScope) => {
            scope.today = () => {
                scope.dt = new Date();
            }
            scope.today();

            scope.clear = () => {
                scope.dt = null;
            }
            scope.disabled = (date: Date, mode) => {
                return (mode == 'day' && (date.getDay() === 0 || date.getDay() === 6)); //disable Saturday and Sunday
            }
            scope.open = ($event) => {
                $event.preventDefault();
                $event.stopPropagation();
                scope.opened = true;
            }

            scope.dateOptions = {
                formatYear: 'yy',
                startingDay: 1
            }

            scope.formats = ['yyyy-MM-dd', 'dd-MMMM-yy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
            scope.format = scope.formats[0];            
        }
        controller = ($scope: IMaDatePickerScope) => {

        }
    }
}

It is taken from the DatePicker section here and is supposed to function like the popup version shown on that page. 它取自此处的DatePicker部分 ,其功能应类似于该页面上显示的弹出版本。 Whenever I click the calendar button to initiate the directive, however, the calendar appears in a broken way. 每当我单击日历按钮启动指令时,日历就会以损坏的方式显示。 Additionally, the calendar keeps expanding to the left when I click on the "next month" arrow in the popup calendar. 此外,当我单击弹出式日历中的“下个月”箭头时,日历会继续向左扩展。 Is there something wrong in my code? 我的代码有问题吗? I didn't add any styling with this, as there doesn't seem to be any according to the site, but I wanted to check if there was anything obviously wrong with the code before taking apart my existing styling. 我没有为此添加任何样式,因为根据站点似乎没有任何样式,但是我想在拆解现有样式之前检查代码中是否有明显错误的地方。

Without seeing which CSS files you may be including, I can't say exactly what the culprit is, although if you are using Twitter Bootstrap along with some other CSS libraries in addition to Angular UI Bootstrap, you most likely have style conflicts from one or more of these other CSS files. 没有看到您可能包含的CSS文件,我无法确切说明罪魁祸首,尽管如果您将Twitter Bootstrap与除Angular UI Bootstrap之外的其他CSS库一起使用,则您很可能会遇到一个或多个样式冲突。这些其他CSS文件更多。 The easiest way I have found to isolate these issues is by using the Chrome browser developer tools to examine the CSS overrides and their sources and see exactly where the conflicts were and then updating my custom CSS to fix them. 我发现隔离这些问题的最简单方法是使用Chrome浏览器开发人员工具检查CSS覆盖及其来源,并准确查看冲突的位置,然后更新我的自定义CSS来解决这些问题。 I don't recommend modifying the OTB CSS libraries like Twitter Bootstrap for maintainability. 我不建议修改Twitter Bootstrap之类的OTB CSS库以实现可维护性。 Always use your own custom CSS file. 始终使用自己的自定义CSS文件。 I did have similar issues with the date picker and it took a little digging to find out where they were. 我在日期选择器上确实遇到了类似的问题,花了一些时间才找出它们在哪里。 It's a hazard of using multiple CSS libraries, if that indeed is what your situation is. 如果确实是您的情况,那么使用多个CSS库是有危险的。 Happy hunting! 狩猎愉快!

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

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