Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js"></script>
  <script src="https://cdn.rawgit.com/philbooth/check-types.js/master/src/check-types.js"></script>  
  <title>Validated constants</title>
<script>
  check.verify.myConstants = function (values) {
    check.verify.object(values, 'missing constants');
    check.verify.unemptyString(values.username, 'need username');  
  };
  
  angular.module('MyApp', [])
  .provider('MyAppConstants', function () {
    // default values
    var values = {
      username: 'World'
    };
    return {
      set: function (constants) {
        check.verify.myConstants(constants);
        angular.extend(values, constants);
        values = Object.freeze(values);
      },
      $get: function () {
        return values;
      }
    };
  })
  .run(function ($rootScope, MyAppConstants) {
    check.verify.myConstants(MyAppConstants, 'MyApp is configured incorrectly');
    $rootScope.username = MyAppConstants.username;
  });
</script>    
</head>
<body ng-app="MyApp">
  <script>
    // override default values
    angular.module('MyApp').config(function (MyAppConstantsProvider) {
      MyAppConstantsProvider.set({
        username: 'Angular'
      });
    });
  </script>
  <p>Hello, {{ username }}</p>
</body>
</html>
Output

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

Dismiss x
public
Bin info
bahmutovpro
0viewers