Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am just now learning Angular.js and i am trying to share data between controllers. Here is where i call myApp from a seperate JS file:

    <div ng-app="myApp">

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

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

    </div>

Now i have a JS file called main which contains this code:

var myApp = angular.module('myApp', []);
myApp.factory('Data',function(){
    return {message:'I am data from a service'}
})

function FirstCtrl($scope, Data){
    $scope.data = Data;
}

function SecondCtrl($scope, Data){
    $scope.data = Data;
}

What I want to happen is the and tags should share whatever is typed into the text boxes. When i load the page i get an error in saying

'Uncaught ReferenceError: angular is not defined main.js:1'

'Uncaught object'

No matter how a try and change the code it will not work. Any ideas?

share|improve this question
2  
have you added a reference to the main.js in the originating html file after the angular.js reference? –  Delta Jun 25 '14 at 20:36
    
@Delta I had referenced my main.js file before angular.js, I switched them around and it worked. I did not realize you had to reference angular.js before any other js files, thought it would be the other way around. Thank you for your help –  mitch Jun 25 '14 at 20:41

1 Answer 1

Have you included the angularjs library in your index.html ? If not, you can use this CDN:

//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js

from http://cdnjs.com/libraries/angular.js/

Include this in the script tag of your index.html

share|improve this answer
    
I use googles CDN, and yes i have it in my file –  mitch Jun 25 '14 at 20:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.