123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div ng-controller="AppCtrl as ctrl" ng-app="MyApp">
  <div layout="row">
    <md-input-container>
      <label>HH:mm</label>
      <input ng-model="ctrl.timeInput" time-input/>
    </md-input-container>
    <md-select data-ng-model="ctrl.ampm" placeholder="AM/PM">
      <md-option ng-repeat="ampm in ctrl.ampms" ng-value="ampm">{{ ampm }}</md-option>
    </md-select>
  </div>
  {{ ctrl.timeInput }} {{ ctrl.ampm }}
    <md-select ng-model="ctrl.ampm" placeholder="AM/PM">
      <md-option ng-repeat="ampm in ['AM', 'PM']" ng-value="ampm">{{ ampm }}</md-option>
    </md-select>

</div>


            
          
!
            
              (function () {
  'use strict';
  angular.module('MyApp')
    .controller('AppCtrl', function() {
      var _this = this;
      _this.timeInput = "";
      _this.ampm = '';
    _this.ampms = ['AM', 'PM'];
    })
    .directive('timeInput', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, controller) {
          var numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':'];
          var preValue = '';
          
          function ensureHttpPrefix(value) {
            if (value && preValue.length < value.length && value.length < 6) {
              var valueArray = value.split('');
              var numberOnlyArray = [];

              // accepts only numbers and colon
              for (var i = 0; i < valueArray.length; i++) {
                if (numbers.indexOf(valueArray[i]) >= 0) {
                  // Colon is accetable at the index at 1 and 2.
                  if (valueArray[i] === ':') {
                    if (i === 1 || i === 2) {
                      numberOnlyArray.push(valueArray[i]);
                    }
                  } else {
                    // Prevent placing number at index at 3
                    if (numberOnlyArray.length === 2) {
                      numberOnlyArray.push(':');
                      numberOnlyArray.push(valueArray[i]);
                    } else {
                      numberOnlyArray.push(valueArray[i]);
                    }
                  }
                }
              }

              if (numberOnlyArray[1] === ':') {
                numberOnlyArray.unshift('0');
              }
              if (numberOnlyArray.length === 2) {
                var hour = parseInt(numberOnlyArray[0]) * 10 + parseInt(numberOnlyArray[1]);
                if (hour > 12) {
                  numberOnlyArray[0] = '1';
                  numberOnlyArray[1] = '2';
                }
                numberOnlyArray.push(':');
              }
              if (numberOnlyArray.length === 4) {
                // When the first digit of minute is entered, automatically put 0 after that.
                numberOnlyArray[4] = 0;
              }
              if (numberOnlyArray.length === 5) {
                var min = parseInt(numberOnlyArray[3]) * 10 + parseInt(numberOnlyArray[4]);
                if (min >= 60) {
                  numberOnlyArray[3] = 5;
                  numberOnlyArray[4] = 9;
                }
              }
              preValue = value;
              controller.$setViewValue(numberOnlyArray.join(""));
              controller.$render();
              return numberOnlyArray.join("");
            } else {
              if (value.length > 5) {
                var sixthChar = value.slice(5, 6);
                if (numbers.indexOf(sixthChar) >= 0 && sixthChar !== ':') {
                  // When sixth character is the number, replace the last character
                  value = value.slice(0, 4) + sixthChar;
                } else {
                  value = value.slice(0, 5);
                }
              }
              preValue = value;
              controller.$setViewValue(value);
              controller.$render();
              return value;              
            }
          }
          controller.$formatters.push(ensureHttpPrefix);
          controller.$parsers.push(ensureHttpPrefix);
        }
    }
  });
})();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console