Search code examples
angularjsangularjs-directivedatepickersemantic-ui

Simentic UI date picker with Angular js ng model


Working on semantic UI date picker with angularjs. Trying to bind ng-model with semantic UI date picker but value of picker is not getting assigned. I have also looked on select of date text box value is not getting filled by semantic control UI. So is there any better approach to get value?

// Code goes here

var app = angular.module('App', [])

app.controller('AppController', function($scope) {

  $('#example2').calendar({
    type: 'date'
  });

  $scope.getDate = function() {
    alert($scope.datevalue)
  }
})
<!DOCTYPE html>
<html>

<head>
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
  <link href="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.css" rel="stylesheet" type="text/css" />
  <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>


  <script src="script.js"></script>
</head>

<body ng-app="App" ng-controller="AppController">
  <h3>Date only</h3>
  <div class="ui calendar" id="example2">
    <div class="ui input left icon">
      <i class="calendar icon"></i>
      <input type="text" placeholder="Date" ng-model="datevalue">
    </div>
  </div>
  <br/>
  <button ng-click="getDate()">Submit</button>
</body>

</html>


Solution

  • That's because you need to notify angular that the model was changed.

    On onChange set the scope's value to the date.

    Lile this:

    // Code goes here
    
    var app = angular.module('App', [])
    
    app.controller('AppController', function($scope) {
    
      $('#example2').calendar({
        type: 'date',
        onChange: function (date,text) {
          $scope.datevalue = date;
        }
      });
    
      $scope.getDate = function() {
        alert($scope.datevalue)
      }
    })
    <!DOCTYPE html>
    <html>
    
    <head>
      <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
      <link href="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.css" rel="stylesheet" type="text/css" />
      <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
      <script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
    
    
      <script src="script.js"></script>
    </head>
    
    <body ng-app="App" ng-controller="AppController">
      <h3>Date only</h3>
      <div class="ui calendar" id="example2">
        <div class="ui input left icon">
          <i class="calendar icon"></i>
          <input type="text" placeholder="Date" ng-model="datevalue">
        </div>
      </div>
      <br/>
      <button ng-click="getDate()">Submit</button>
    </body>
    
    </html>

    source

    Note you can wrap this with a directive if it's so important.