Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
    <!-- Twitter bootstrap -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <!-- apiCheck is used by formly to validate its api -->
    <script src="//rawgit.com/kentcdodds/api-check/latest/dist/api-check.js"></script>
    <!-- This is the latest version of angular (at the time this template was created) -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
    <!-- This is the current state of master for formly core. -->
    <script src="https://rawgit.com/formly-js/angular-formly/latest/dist/formly.js"></script>
    <!-- This is the current state of master for the bootstrap templates -->
    <script src="//rawgit.com/formly-js/angular-formly-templates-bootstrap/latest/dist/angular-formly-templates-bootstrap.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.3/ui-bootstrap-tpls.min.js"></script>
    <title>Angular Formly Example</title>
  </head>
  <body ng-app="formlyExample" ng-controller="MainCtrl as vm">
    <div>
      <h1>angular-formly example: {{vm.exampleTitle}}</h1>
      <div>
        This is a <strong>very</strong> basic example of how to use the
        <a href="https://angular-ui.github.io/bootstrap/#/datepicker">UI Boostrap datepicker</a>
        with angular-formly.
      </div>
      <hr />
      <form ng-submit="vm.onSubmit()" name="vm.form" novalidate>
        <formly-form options="vm.options" model="vm.model" fields="vm.fields" options="vm.options">
          <button type="submit" class="btn btn-primary submit-button" ng-disabled="vm.form.$invalid">Submit</button>
          <button type="button" class="btn btn-default" ng-click="vm.options.resetModel()">Reset</button>
        </formly-form>
      </form>
      <hr />
      <h2>Model Value</h2>
      <pre>{{vm.model | json}}</pre>
      <h2>Fields <small>(note, functions are not shown)</small></h2>
      <pre>{{vm.originalFields | json}}</pre>
      <h2>Form</h2>
      <pre>{{vm.form | json}}</pre>
    </div>
    <div style="margin-top:30px">
      <small>
        This is an example for the
        <a href="https://formly-js.github.io/angular-formly">angular-formly</a>
        project made with ♥ by
        <strong>
          <span ng-if="!vm.author.name || !vm.author.url">
            {{vm.author.name || 'anonymous'}}
          </span>
          <a ng-if="vm.author.url" ng-href="{{::vm.author.url}}">
            {{vm.author.name}}
          </a>
        </strong>
        <br />
        This example is running angular version "{{vm.env.angularVersion}}" and formly version "{{vm.env.formlyVersion}}"
      </small>
    </div>
    <!-- Put custom templates here -->
    <script type="text/ng-template" id="datepicker.html">
      <p class="input-group">
        <input  type="text"
                id="{{::id}}"
                name="{{::id}}"
                ng-model="model[options.key]"
                class="form-control"
                ng-click="datepicker.open($event)"
                datepicker-popup="{{to.datepickerOptions.format}}"
                is-open="datepicker.opened"
                datepicker-options="to.datepickerOptions" />
        <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="datepicker.open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
        </span>
      </p>
    </script>
  </body>
</html>
 
body {
  margin: 20px
}
.formly-field {
  margin-bottom: 16px;
}
 
/* global angular */
(function() {
  
  
  var foo = {
    bar : {
      name : 'Danny'
    }
  };
  
  var name = foo.bar && foo.bar.name || 'Paul';
  
  console.log(name);
  
  
  
  'use strict';
  var app = angular.module('formlyExample', ['formly', 'formlyBootstrap', 'ui.bootstrap']);
  app.run(function(formlyConfig) {
  var attributes = [
    'date-disabled',
    'custom-class',
    'show-weeks',
    'starting-day',
    'init-date',
    'min-mode',
    'max-mode',
    'format-day',
    'format-month',
    'format-year',
    'format-day-header',
    'format-day-title',
    'format-month-title',
    'year-range',
    'shortcut-propagation',
    'datepicker-popup',
    'show-button-bar',
    'current-text',
    'clear-text',
    'close-text',
    'close-on-date-selection',
    'datepicker-append-to-body'
  ];
  var bindings = [
    'datepicker-mode',
    'min-date',
    'max-date'
  ];
  var ngModelAttrs = {};
  angular.forEach(attributes, function(attr) {
    ngModelAttrs[camelize(attr)] = {attribute: attr};
  });
  angular.forEach(bindings, function(binding) {
    ngModelAttrs[camelize(binding)] = {bound: binding};
  });
  console.log(ngModelAttrs);
  
  formlyConfig.setType({
    name: 'datepicker',
    templateUrl:  'datepicker.html',
    wrapper: ['bootstrapLabel', 'bootstrapHasError'],
    defaultOptions: {
      ngModelAttrs: ngModelAttrs,
      templateOptions: {
        datepickerOptions: {
          format: 'MM.dd.yyyy',
          initDate: new Date()
        }
      }
    },
    controller: ['$scope', function ($scope) {
      $scope.datepicker = {};
      $scope.datepicker.opened = false;
      $scope.datepicker.open = function ($event) {
        $scope.datepicker.opened = true;
      };
      
    }]
  });
  function camelize(string) {
    string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) {
      return chr ? chr.toUpperCase() : '';
    });
    // Ensure 1st char is always lowercase
    return string.replace(/^([A-Z])/, function(match, chr) {
      return chr ? chr.toLowerCase() : '';
    });
  }
});
  app.controller('MainCtrl', function MainCtrl(formlyVersion) {
    var vm = this;
    // funcation assignment
    vm.onSubmit = onSubmit;
    // variable assignment
    vm.author = { // optionally fill in your info below :-)
      name: 'Kent C. Dodds',
      url: 'https://twitter.com/kentcdodds'
    };
    vm.exampleTitle = 'UI Bootstrap Datepicker'; // add this
    vm.env = {
      angularVersion: angular.version.full,
      formlyVersion: formlyVersion
    };
    vm.model = {
      'date1' : '2015-09-16T23:00:00.000Z'
    };
    vm.options = {};
    vm.fields = [
      {
        key: 'date1',
        type: 'datepicker',
        templateOptions: {
          label: 'Date',
          type: 'text',
          datepickerPopup: 'dd-MMMM-yyyy'
        }
      },
      {
        key: 'date2',
        type: 'datepicker',
        templateOptions: {
          label: 'Date',
          type: 'text',
          datepickerPopup: 'dd-MMMM-yyyy'
        },
        defaultValue: '2015-10-23T23:00:00.000Z'
      },
      {
        key: 'text',
        type: 'input',
        templateOptions: {
          label: 'Name',
          type: 'text'
        },
        defaultValue: 'Bob'
      }
    ];
    vm.originalFields = angular.copy(vm.fields);
    // function definition
    function onSubmit() {
      vm.options.updateInitialValue();
      alert(JSON.stringify(vm.model), null, 2);
    }
  });
})();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
dannycallaghanpro
0viewers