Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabrielem/e4eafb0fc56458b89a0f95de4a854b5b to your computer and use it in GitHub Desktop.
Save gabrielem/e4eafb0fc56458b89a0f95de4a854b5b to your computer and use it in GitHub Desktop.
Postponed AngularJS bootstrap for Truffle since version 2.0.8

Truffle since release 2.0.8 delays the initialization of the global variable web3 after the load event, so this variable is not available during angular bootstrap, inside a .config() section, inside providers and neither inside the controllers initialized at load time and related injected services.

Accessing to the web3 variable inside a setTimeout() call could be a solution for simple projects but the only way to be sure that web3 is initialized is to postpone the angular bootstrap.

Looks at the code below:

<!DOCTYPE html>
<html>
<head>
  ...
</head>
<body ng-cloak>
  ...
  <script>
    window.addEventListener('load', function() {
      angular.bootstrap(document, ['sampleApp'])
    })
  </script>

</body>
</html>

Notice that there is no ng-app directive, the bootstrap is delegated to the small script in the body section. Notice also the ng-cloack directive, it avoids the rendering of the raw page template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment