Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

glenn romaniuk
glenn romaniuk
32,520 Points

Angular - Can't require UUID in a Controller

I have the following code. I need to generate a UUID. I'm trying to require the middleware but it won't let me. It works from the app.js. Whats is the technique to require that middleware for use in my controller?

'use strict';

//var uuid = require('uuid'); i've tried here

angular.module('myApp') .controller('foosballCtrl', function($scope, $log, $interval, dataService){

var uuid = require('uuid'); // here also does not work

dataService.getPlayers(function(response){ var players = response.data.players; $scope.players = players; });

$scope.addPlayer = function() { $scope.players.unshift({name: "Enter New Player Name"}); };

$scope.team1Won = function() { //$log.log("team 1 won " + uuid.v4()); };

$scope.team2Won = function() { $log.log("team 2 won"); };

})

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

Because angular is browser-side instead of node you need to include uuid as a script, not a require() (unless you've included a require.js script already to use it in browser as well).

From node-uuid's README:

Install it in your browser:

<script src="uuid.js"></script>