boindiil
boindiil

Reputation: 5865

Angular UI Bootstrap - Datepicker show on button click without function

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

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>

Upvotes: 2

Views: 7961

Answers (1)

boindiil
boindiil

Reputation: 5865

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>

Upvotes: 5

Related Questions