1

I have the following html:

<!DOCTYPE html>
<html>
<head>
    <title>Angular JS</title>
    <script type="text/javascript" src="/javascripts/app.js"></script>
</head>
<body>

<div ng-app="myApp">

    <div ng-controller="FirstController">
        <input type="text" ng-model="data.message">
        <h1>{{data.message}}</h1>
    </div>


    <div ng-controller="SecondController">
        <input type="text" ng-model="data.message">
        <h1>{{data.message}}</h1>
    </div>

</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
</body>
</html>

and the following coffeescript:

window.myApp = angular.module 'myApp', []

myApp.factory 'Data', ->
    return message : 'I am data from a factory'

myApp.controller 'FirstController', ($scope, Data) ->
    $scope.data = Data

myApp.controller 'SecondController', ($scope, Data) ->
    $scope.data = Data

When I run the app, it prints out {{data.message}} instead of the text I type in the input box. When I remove the module dependency from the html and get data from a parent within the div, it works fine. This leads me to believe that the module is not getting created. What is the problem with my code?

3
  • 2
    Try putting the script tag in the head after the library script. Commented Sep 21, 2013 at 19:08
  • Do you get any output on the JavaScript console? Commented Sep 21, 2013 at 20:44
  • @fncombo I don't know why it didn't work earlier, but it works now. If you submit your comment as an answer, I'll accept it. Thanks so much! Commented Sep 21, 2013 at 20:56

1 Answer 1

4

You need to put the script tag that's in the head after the library script.

Sign up to request clarification or add additional context in comments.

Comments

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.