0

I have following controller defined in an external file but when I try to run my node server I get 'angular is not defined' error.

vaultcontroller.js

angular.module('demoapp.vaultcontroller', [])
.controller('vaultcontroller', function($scope, appFactory){

    console.log("INSIDE APP CONTROLLER.");

    $("#success_holder").hide();
    $("#success_create").hide();
    $("#error_holder").hide();
    $("#error_query").hide();

    $scope.get_all_currencies_from_vault = function(){
        console.log("VAULT CONTROLLER IS CALLED -1 ");
        appFactory.queryAllTuna(function(data){
            var array = [];
            for (var i = 0; i < data.length; i++){
                parseInt(data[i].Key);
                data[i].Record.Key = parseInt(data[i].Key);
                array.push(data[i].Record);
            }
            array.sort(function(a, b) {
                return parseFloat(a.Key) - parseFloat(b.Key);
            });
            $scope.all_tuna = array;
        });
    }
});

Here is the package.json

"dependencies": {
    "angular": "1.6.5",
    "angular-route": "1.6.5",
    "body-parser": "latest",
    "ejs": "latest",
    "express": "latest",
    "fabric-ca-client": "^1.0.2",
    "fabric-client": "^1.0.2",
    "grpc": "1.11.0",
    "morgan": "1.9.0"
},

This is the error trace:

angular.module('demoapp.vaultcontroller', [])
^

ReferenceError: angular is not defined
    at Object.<anonymous> (D:\Project-code\demo-POC\demopoc\client\vaultcontroller.js:2:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\Project-code\demo-POC\demopoc\client\routes.js:5:23)
    at Module._compile (module.js:652:30)

Here is my index.html imports

<!-- require jquery and bootstrap scripts -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular-route.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

Update-1 Loading vaultcontroller

in app.js file, I did following.

.when('/vault', {
  templateUrl: 'vault.html',
  controller: 'vaultcontroller'
})

And the code for vaultcontroller is posted above. Then I have done following in app.js

var demoapp = angular.module('application', ["ngRoute", "application.vaultcontroller"]);

And then imported the file in the index.html using script.

<!-- requiring the angular page -->
<script type="text/javascript" src="app.js"> </script>
<script type="text/javascript" src="vaultcontroller.js"> </script>

If any more code required please let me know. New to angular

Update-2 > Solution Ok, I got some help from @Aleksey Solovey and as he suggested I installed a separate server http-server for delivering my static files, earlier my node server was delivering static files as well. I removed that part from the server.js and I didn't get any of the above errors, it started working. And I have now two URLs 1. for static content > localhost:8080 2. For API server (written in node, uses server.js for startup) > localhost:8000

Thank you for helping out.

15
  • I have added the script in index.js, it is already there in the html page. Commented May 3, 2018 at 12:09
  • Wait, do I need to add it in each and every page? I have one main index.html inside which I have a <div ng-view>, index.html contains all the files. Even the file of controller is also there. Commented May 3, 2018 at 12:11
  • how are you loading your demoapp.vaultcontroller module? Commented May 3, 2018 at 12:32
  • I have updated my question, I hope that is what you meant by loading of module. Commented May 3, 2018 at 12:39
  • I can't immediately see what's wrong, but a simple answer: your scripts cannot see angular.js (to find the reference for the angular variable), which means they are loaded before it. Does vaultcontroller.js script comes after angular.js script? Commented May 3, 2018 at 12:44

1 Answer 1

0

You Need to Add the Blow

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Velu for answering I have imports already defined in the main index.html page.

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.