1

I'm new to both JavaScript & AngularJS. I'm building an app which talks to a building management system: it has a module called 'JaceMod' containing a service to contact the BMS and retrieve data from it. I'm adding functionality to graph data using Dygraphs, and I've created a directive to instantiate a graph and populate it with data.

I would like to put my Dygraphs directive into a module called 'dygraphs', in a separate file 'angular-dygraphs.js', so I can re-use it later, and require this module as a dependency in 'JaceMod'. Like this:

'use strict';
console.log('Loading angular-dygraphs.js');
angular.module('dygraphs', []);

angular.module('dygraphs').directive('mrhDygraph', function ($parse) {
    return {
...etc ...
    };
});

And then the main file 'app.js':

'use strict';
console.log('Loading app.js');

angular.module('JaceMod', ['dygraphs']);

angular.module('JaceMod').factory('JaceData', function ($http, $timeout) {
    var JaceDataService = {};
...etc ...
    return JaceDataService;
});

angular.module('JaceMod').controller('myCtrl', function myCtrl($scope, JaceData) {
    JaceData.initialise($scope);
    JaceData.subscribe($scope);
});

The html looks like this:

<html lang="en" ng-app="JaceMod">
<head>
    <meta charset="utf-8">
    <title>AngularJACE</title>
    <style type="text/css">
        body {font-family:  Trebuchet MS,Helvetica,Arial,sans-serif; }
    </style>
</head>
<body ng-controller="myCtrl">
    <div mrh-dygraph="graphData"
         mrh-dygraph-options="{xlabel: 'Local Time', legend: 'always',
                              showRangeSelector: true, labels: ['Time', 'Outside'],
                              ylabel: 'Temperature (ºC)', height: 420}"
         style="width: 100%; height 420px;"/>
    <script src="dygraph-combined.js"></script>
    <script src="angular.js"></script>
    <script src="angular-dygraphs.js"/>
    <script src="app.js"></script>
</body>

But when I load this html, I see my console message 'Loading angular-dygraphs.js', followed by an eror 'Error: No module: JaceMod'. I never see the log message in app.js. If I switch the load order of the last 2 javascript files round, I see that app.js loads and the error message is 'Error: No module: dygraphs'. The second listed file seems never to be loaded.

If I copy the code from angular-dygraphs.js into app.js, leaving the directive in its own module, it works.

Why is this happening? Is it not possible to put modules in their own files, or something? The documents haven't been much help...

EDITED TO ADD

The html & js files are all in the same directory, including the dygraphs & angular libraries. I'm loading the html from file, no server involved.

UPDATE

I copied everything from angular-dygraphs.js into app.js, and commented out the entirety of angular-dygraphs.js: still getting the problem, app.js does not load.

2
  • Are you sure there's not an error in app.js somewhere? Do you ever see the Loading app.js message? Can you create a plunker showing the problem? Commented Jun 19, 2013 at 23:12
  • As far as I can tell there is no error in app.js. I see the 'loading' message from whichever file is loaded first, and if I copy & paste the contents of angular-dygraphs.js into app.js I see both loading messages and it works as expected - the graph is shown and updates as live data is received. Like I said, I'm new to Javascript & I have no idea how to create a plunker :-( I'll look into it though. Commented Jun 20, 2013 at 6:52

1 Answer 1

6

Your angular-dygraphs.js script tag isn't closed so it never reads app.js. Script tags cannot be self-closing:

<script src="angular-dygraphs.js"/>

should be

<script src="angular-dygraphs.js"></script>
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't know that: however I've changed over to an explicit end tag & it made no difference. I'm using Safari 5.1.9 on Mac OS 10.6.8 if that makes a difference.
@Max Try to add type="text/javascript" to the tag as well.
@Ken - attribute added, no difference as far as I can see
This was the trouble: when I changed to the end tag, I mistyped it as '<script/>', instead of '</script>', and Safari silently did the unclosed tag thing, thereby hiding my gaffe. Duh! And when I edited the question, I didn't make the same mistake.
Another thing: according to Flanagan, in XHTML a script tag can be self closing: however I changed the Doctype to strict XHTML & reverted the tag, and still got the error (the original Doctype was HTML 5)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.