Datepicker not opening twice in angular-ui version 0.11.0

前端 未结 11 1442
一向
一向 2020-12-28 12:43

I am trying to have 2 datepickers and I am using Angular UI version 0.11.0.

My HTML code


     

        
相关标签:
11条回答
  • 2020-12-28 12:53

    I had the same problem, but by simply putting the "opened" boolean var in an object solved the problem for me:

    < .. is-open="datePicker.opened" >
    ...
    $scope.datePicker = {opened:false};
    $scope.openDate = function($event) {
         $event.preventDefault();
         $event.stopPropagation();
         $scope.datePicker.opened = true;
    };
    

    I have not used angular for that long but I think this is scope problem and then I learned that it is always good to have "a dot in the variable name"... ( datePicker.opened )

    (I now see a post above with a similar solution. But I did not need to use the timeout. This code was enough.)

    0 讨论(0)
  • 2020-12-28 12:57

    Here is the explanation about this behaviour

    AngularJS MTV Meetup: Best Practices (2012/12/11)

    https://www.youtube.com/watch?feature=player_detailpage&v=ZhfUv0spHCY#t=1870

    you can write it like this.

     <input type="text" datepicker-popup="dd-MMMM-yyyy" ng-model="cdate.customStartDate" is-open="date_picker1.opened"  max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" class="input-md" />
    

    In controller:

    $scope.date_picker1 ={
        date: new Date(),
        opened: false;
     };
     $scope.open = function($event) {
         .....
         $scope.date_picker1.opened = true;
     };
    
    0 讨论(0)
  • 2020-12-28 12:58

    Found answer on other StackOverflow question, just use is-open="$parent.isOpen"

    https://stackoverflow.com/a/20213924/1596661

    0 讨论(0)
  • 2020-12-28 13:02

    I have the easiest, one-line solution that does not require container objects, function calls or other hassles like preventDefault. You don't even have to declare this in scope because undefined is evaluated as false.

    ...
      ng-click="dateOpened = !dateOpened"
    ...
    

    I tested this with angular-ui 0.13.0 (Angular Bootstrap). This works because the invocation of ng-click is already trapping the default event.

    0 讨论(0)
  • 2020-12-28 13:03

    I was having the same issue whereby I could only open the date picker control once using the button, but it would not open a second time. The problem may be related to a scope issue which might be coming about because the button is not a child of the input HTML element. I was able to get the button to work by changing the data model a little bit. For example, instead of using $scope.isDatePickerOpen as the model, I changed to $scope.datePicker.isOpen (and set is-open="datePicker.isOpen"). Note that the new data model for is-open does not hang directly off of $scope, but was moved one level deep (off the $scope.datePicker object). This seems to make the data more "findable".

    The other thing I had to do was change the data model on a timer. For example:

    $scope.openDatePicker = function($event) {
      $event.preventDefault();
      $event.stopPropagation();
      $timeout( function(){
         $scope.datePicker.isOpen = true;  
      }, 50);
    };
    

    At any rate, your workaround posted above was what gave me the motivation to keep looking for a solution, so thanks!

    0 讨论(0)
  • 2020-12-28 13:10

    Quick Fix: Removed the button tag altogether and modified the datepicker code, so it looks like this :

    <input type="text" 
           datepicker-popup="dd-MMMM-yyyy"
           ng-model="cdate.customStartDate"
           is-open="cdate.customStartDate.open"
           ng-click = "cdate.customStartDate.open = true"
           max-date="maxDate"
           datepicker-options="dateOptions"
           date-disabled="disabled(date, mode)" 
           ng-required="true"
           close-text="Close"
           class="input-md" />
    
    0 讨论(0)
提交回复
热议问题