Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
    
    
    
    <!-- Angular Material CSS now available via Google CDN; version 0.10 used here -->
    <link href="//ajax.googleapis.com/ajax/libs/angular_material/0.11.4/angular-material.min.css" rel="stylesheet">
    <!-- apiCheck is used by formly to validate its api -->
    <script src="//npmcdn.com/api-check@latest/dist/api-check.js"></script>
    
    <!-- Angular Material Dependencies -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-aria.js"></script>
    
    <!-- Angular Material Javascript now available via Google CDN; version 0.10 used here -->
    <script src="//ajax.googleapis.com/ajax/libs/angular_material/0.11.4/angular-material.min.js"></script>
    <!-- This is the latest version of formly core. -->
    <script src="//npmcdn.com/angular-formly@latest/dist/formly.js"></script>
    <title>Angular Formly Example</title>
  </head>
  <body ng-app="formlyExample" ng-controller="MainCtrl as vm" class="icondemoFontIconsWithClassnames">
    <div>
      <h1>angular-formly example: {{vm.exampleTitle}}</h1>
      <div>
        This example shows some integration with <a href="https://material.angularjs.org">Angular Material</a>.
        There is a work-in-progress library out there called
        <a href="https://github.com/formly-js/angular-formly-templates-material">angular-formly-templates-material</a>,
        but it isn't ready yet so this example is just to show how to get started. If you're interested in contributing
        to a full-on library, please comment
        <a href="https://github.com/formly-js/angular-formly-templates-material/issues/1">here</a>
      </div>
      <hr />
      <h4>vm.options:</h4>
      <pre>{{vm.options | json}} </pre>
      <hr/>
      
      <form ng-submit="vm.onSubmit()" name="vm.form" novalidate>
        <formly-form model="vm.model" fields="vm.fields" options="vm.options" form="vm.form">
          <md-button type="submit" class="md-raised md-primary" ng-disabled="vm.form.$invalid">Submit</md-button>
          <md-button type="button" class="md-raised" ng-click="vm.options.resetModel()">Reset</md-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 -->
  </body>
</html>
 
body {
  margin: 20px
}
.formly-field {
  margin-bottom: 16px;
}
/* for icons. Copied from angular-material's website */
@font-face {
  font-family:"icomoon";
  src:url("https://cdn.rawgit.com/angular/material/master/docs/app/fonts/icomoon.eot");
  font-weight:normal;
  font-style:normal;
}
@font-face {
  font-family: 'icomoon';
  src:url('https://rawgit.com/angular/material/master/docs/app/fonts/icomoon.eot?-cmq1um');
  src:url('https://cdn.rawgit.com/angular/material/master/docs/app/fonts/icomoon.eot?#iefix-cmq1um') format('embedded-opentype'),
    url('https://cdn.rawgit.com/angular/material/master/docs/app/fonts/icomoon.woff?-cmq1um') format('woff'),
    url('https://cdn.rawgit.com/angular/material/master/docs/app/fonts/icomoon.ttf?-cmq1um') format('truetype'),
    url('https://cdn.rawgit.com/angular/material/master/docs/app/fonts/icomoon.svg?-cmq1um#icomoon') format('svg');
  font-weight: normal;
  font-style: normal;
}
 
/* global angular */
(function() {
  console.clear();
  
  'use strict';
  var app = angular.module('formlyExample', ['formly', 'ngAria', 'ngAnimate', 'ngMaterial']);
  
  app.run(function(formlyConfig) {
    formlyConfig.setType({
      name: 'input',
      template: '<input ng-model="model[options.key]">'
    });
    
    formlyConfig.setType({
      name: 'checkbox',
      template: '<md-checkbox ng-model="model[options.key]">{{to.label}}</md-checkbox>'
    });
    
    formlyConfig.setWrapper({
      name: 'mdLabel',
      types: ['input'],
      template: '<label>{{to.label}}</label><formly-transclude></formly-transclude>'
    });
    
    formlyConfig.setWrapper({
      name: 'mdInputContainer',
      types: ['input'],
      template: '<md-input-container><formly-transclude></formly-transclude></md-input-container>'
    });
    
    // having trouble getting icons to work.
    // Feel free to clone this jsbin, fix it, and make a PR to the website repo: https://github.com/formly-js/angular-formly-website
    formlyConfig.templateManipulators.preWrapper.push(function(template, options) {
      if (!options.data.icon) {
        return template;
      }
      return '<md-icon class="step" md-font-icon="icon-' + options.data.icon + '"></md-icon>' + template;
    });
  });
  
  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 = 'angular-material'; // add this
    vm.env = {
      angularVersion: angular.version.full,
      formlyVersion: formlyVersion
    };
    vm.model = {};
    
    vm.options = {
      formState: {
        disabled: true
      }
    };
    
    vm.fields = [
      {
        key: 'text',
        type: 'input',
        templateOptions: {
          label: 'Input'
        }
      },
      {
        elementAttributes: {
          layout: 'row',
          'layout-sm': 'column'
        },
        fieldGroup: [
          {
            key: 'firstName',
            className: 'flex',
            type: 'input',
            templateOptions: {
              label: 'First Name'
            }
          },
          {
            key: 'lastName',
            className: 'flex',
            type: 'input',
            templateOptions: {
              label: 'Last Name'
            }
          }
        ]
      },
      {
        key: 'knowsMuffinMan',
        type: 'checkbox',
        templateOptions: {
          label: 'Do you know the muffin man?'
        }
      }
    ];
    
    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
tadeussenfpro
0viewers