2

I want to show the datepicker from the angular ui bootstrap library when one of the following events occours:

  • User clicks in input box
  • User clicks on calendar icon

I know that it is possible to do this by calling an controller function when the button is clicked like in the example here

My question is: Is it really necessary to create a controller/scope method to show the datepicker?

The following example shows my code:

angular.module("MyModule", ["ui.bootstrap"]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.js"></script>

<div ng-app="MyModule">
  <div class="form-group">
    <label for="BirthDate" class="control-label col-sm-4">BirthDate</label>
    <div class="col-sm-6">
      <p class="input-group">
        <input 
               type="text" 
               class="form-control" 
               id="BirthDate" 
               ng-model="NewEmployee.BirthDate" 
               datepicker-popup="dd.MM.yyyy" 
               is-open="Opened" 
               ng-click="Opened=true">
        <span class="input-group-btn">
          <button type="button" class="btn btn-default" ng-click="Opened=true">
            <i class="glyphicon glyphicon-calendar"></i>
          </button>
        </span>
      </p>
    </div>
  </div>
 </div>

1 Answer 1

5

I found out that it is possible to prevent the event propagation within the ng-click code like this:

angular.module("MyModule", ["ui.bootstrap"]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.js"></script>

<div ng-app="MyModule">
  <div class="form-group">
    <label for="BirthDate" class="control-label col-sm-4">BirthDate</label>
    <div class="col-sm-6">
      <p class="input-group">
        <input 
               type="text" 
               class="form-control" 
               id="BirthDate" 
               ng-model="NewEmployee.BirthDate" 
               datepicker-popup="dd.MM.yyyy" 
               is-open="Opened" 
               ng-click="Opened=true">
        <span class="input-group-btn">
          <button type="button" class="btn btn-default" ng-click="Opened=true;$event.stopPropagation();">
            <i class="glyphicon glyphicon-calendar"></i>
          </button>
        </span>
      </p>
    </div>
  </div>
 </div>

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

Comments

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.