1

From the example of UI Bootstrap of Angularjs I managed to create UI datepicker using following code.

<div class="col-md-2">
 <p class="input-group">
    <input type="text" class="text-box input-large input-large-altered" name="HandOverToOwner" datepicker-popup="dd.MM.yyyy"  ng-model="project.dt" is-open="opened" ng-required="true"  />
          <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>
  <span class="help-block" ng-show="ProjectCreate.HandOverToOwner.$error.required">Required*</span>
</div>

Now I want to get rid of calendar button and enable popup the calendar when edit box is selected. How do I replace this is-open="opened" attribute?

app.controller('createProjectController', ['$scope', function ($scope, $timeout) {

     $scope.project = {};
       $scope.clear = function () {
            $scope.project.dt = null;
        };
        $scope.open = function ($event) {
            $event.preventDefault();
            $event.stopPropagation();
            $scope.opened = true;
        };

        $scope.format = 'dd.MM.yy';

        $scope.dateOptions = {
            formatYear: 'yy',
        };
    }]);

1 Answer 1

3

If I have understood your question right and you want the datepicker to be triggered when you click in the input field, you need to add ng-click=open($event) in your <input>element.

<div class="col-md-2">
 <p class="input-group">
    <input type="text" class="text-box input-large input-large-altered" name="HandOverToOwner" datepicker-popup="dd.MM.yyyy"  ng-model="project.dt" is-open="opened" ng-click="open($event)" ng-required="true"  />
  </p>
  <span class="help-block" ng-show="ProjectCreate.HandOverToOwner.$error.required">Required*</span>
</div>

See plunker

EDIT

<div class="col-md-2">
 <p class="input-group">
    <input type="text" class="text-box input-large input-large-altered" name="HandOverToOwner" datepicker-popup="dd.MM.yyyy"  ng-model="project.dt1" is-open="opened['id1']" ng-click="open($event, 'id1')" ng-required="true"  />
  </p>
  <p>
    <input type="text" class="text-box input-large input-large-altered" name="HandOverToOwner" datepicker-popup="dd.MM.yyyy"  ng-model="project.dt2" is-open="opened['id2']" ng-click="open($event, 'id2')" ng-required="true"  />
  </p>
  <span class="help-block" ng-show="ProjectCreate.HandOverToOwner.$error.required">Required*</span>
</div>

In js file

$scope.opened = [] # initalize open as array somewhere in your controller

$scope.open = function ($event, id) {
  $event.preventDefault();
  $event.stopPropagation();
  $scope.opened[id] = true;
};

new plunker

Sign up to request clarification or add additional context in comments.

5 Comments

Hello thanks for your response. But in this case If I have multiple datepicker in same form, by clicking one it opens the popup in all box. In other case I will have to write function for each text-box. Is there any way without ng-click="open($event)" attribute.
Ok. Added a possible (but maybe a little messy) way to use multiple datepickers. The solution is to use an array for $scope.opened and use this in is-open in html. Added a plunker for this as well.
This solved the problem.. Thanks. However I noticed that if I switch to version 0.10.0 is-open is not required for ng-click attribute is not required anymore. I don't why :) But for now I will take your solution. Thanks for helping this newbie...
@Hilde I am curious to see your answer in the plunkler but unfortunately the link doesn't work :(
@MuhammedAthimannil It seems that the plunkers are gone, yes. Thx for providing me feedback on taht. I will provide some new as soon as I can! I'll let u know when it's fixed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.