简体   繁体   English

angular-ui:显示按钮时datepicker似乎挂angularjs

[英]angular-ui: datepicker seems to hang angularjs when showing button

I have this piece of html code in my application (the ng-app and ng-controller values are defined before): 我的应用程序中有这段html代码(之前定义了ng-app和ng-controller值):

<div>
    <label for="projectSearchDateFrom"><%= Res("Projects.Search.From")%></label>
    <input id="projectSearchDateFrom" type="text" ng-model="startDate" ui-date="dateOptions"/>
    <img ng-show="hasStartDate()" ng-click="clearStartDate()" src="/_Layouts/ClientPortal/Images/layout/TA/delete-small.png" alt="<%= Res("ToolbarDialog.Clear")%> <%= Res("Projects.Search.From")%>" title="<%= Res("ToolbarDialog.Clear")%>" />
</div>

My AngularJS controller looks like this: 我的AngularJS控制器如下所示:

function ProjectSearchCtrl($scope) {
    $scope.startDate = '';

    $scope.hasStartDate = function () {
        return $scope.startDate != '';
    };

    $scope.clearStartDate = function () {
        $scope.startDate = '';
    };

    $scope.dateOptions = {
        dateFormat: "yy-mm-dd",
        showOn: "focus"
    };
}

This works perfectly: I have a datepicker set up correctly thanks to AngularUI, the AngularJS binding works... 这完美地工作:感谢AngularUI,我正确设置了一个日期选择器,AngularJS绑定可以工作...

But if I change the showOn value to "button" or "both" (the two possible options which will actually show the datepicker button), everything after the input (containing the ui-date attribute) stops working: ng-show, ng-click... The controller doesn't even get called. 但是,如果我将showOn值更改为“ button”或“ both”(实际上将显示datepicker按钮的两个可能的选项),则输入后的所有内容(包含ui-date属性)都将停止工作:ng-show,ng-单击...控制器甚至都不会被调用。

Versions (all is up-to-date): 版本(所有都是最新的):

  • jQuery 1.7.2 jQuery 1.7.2
  • angularJS 1.0.0 angularJS 1.0.0
  • angularUI 0.1.0 angularUI 0.1.0
  • Chrome 20 铬20

Please take a look at this line in the Select2 directive . 在Select2指令中查看这一行 This is a note to ANYONE writing a directive / implementing a plugin in AngularJS (not just AngularUI): 这是对任何人在AngularJS(不仅仅是AngularUI)中编写指令/实现插件的说明:

Any plugin that injects a new DOM element immediately after the linked element runs the risk of disrupting the compiler. 任何在链接元素之后立即插入新DOM元素的插件都可能会破坏编译器。 The reason is because the way AngularJS works, it caches the index of each DOM element at compile time, and then makes a second pass upon linking. 原因是因为AngularJS的工作方式是,它在编译时缓存每个DOM元素的索引,然后在链接时进行第二次传递。 When you inject new DOM, you offset the index of all siblings immediately after the directive. 注入新的DOM时,将在指令之后立即偏移所有同级的索引。

For this reason, I've been forced to wrap both TinyMCE and Select2 in a setTimeout so that the DOM is injected after the linking is done. 因此,我不得不将TinyMCE和Select2都包装在setTimeout中,以便在完成链接注入DOM。 Note that I don't bother using $timeout because I really don't need/want $apply() to fire just to turn on the plugin, as there are already callbacks in place that do this when the plugin changes the data. 请注意,我不必费心使用$ timeout,因为我真的不需要/想要$ apply()只是为了打开插件就触发,因为已经有回调在插件更改数据时执行。

I'll look into making sure this is uniform across AngularUI. 我将研究确保这在AngularUI中是统一的。 Unfortunately, there appears to be no elegant solution to this problem in AngularJS at this time, however it's a problem I've been thinking about for some time and am constantly looking for a better solution towards. 不幸的是,目前在AngularJS中似乎没有针对此问题的完美解决方案,但是我已经思考了一段时间,并且一直在寻找更好的解决方案。

Read this Google Groups post for more information about compiling vs linking: https://groups.google.com/forum/?fromgroups#!searchin/angular/compile$20link/angular/RuWn5W3Q5I0/KJhcQJ_RNsIJ 阅读此Google网上论坛帖子,以获取有关编译和链接的更多信息: https : //groups.google.com/forum/? fromgroups#!searchin/angular/ compile $20link/angular/RuWn5W3Q5I0/ KJhcQJ_RNsIJ

You can also open a bug ticket on the AngularUI project in the future. 您将来也可以在AngularUI项目上打开错误凭单

As suggested by Pete BD in his comment on the question, there is some kind of bug/unwanted behaviour in the way that jQueryUI and angularJS interact. 正如Pete BD在对该问题的评论中所建议的那样,jQueryUI和angularJS交互的方式存在某种错误/有害行为。 A workaround is to wrap the input control in a div. 一种解决方法是将输入控件包装在div中。

<div class="date">
    <label for="projectSearchDateFrom"><%= Res("Projects.Search.From")%></label>
    <div>
        <input id="projectSearchDateFrom" type="text" ng-model="startDate" ui-date="dateOptions"/>                                  
    </div>
    <img class="clear" ng-show="hasStartDate()" ng-click="clearStartDate()" src="/_Layouts/ClientPortal/Images/layout/TA/delete-small.png" alt="<%= Res("ToolbarDialog.Clear")%> <%= Res("Projects.Search.From")%>" title="<%= Res("ToolbarDialog.Clear")%>" />
</div>

Now I can use showOn "both" or "button". 现在,我可以使用showOn的“两个”或“按钮”。

此问题已在最新版本中修复!

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

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