Issue: [ng:areq] The argument 'HelloController' is not defined as a function, it is displayed as undefined

Struggling to integrate Angular with Django has been quite a challenge for me. After finally managing to make it work, I encountered yet another error. Each time I load the application, the following error message pops up:

Error: [ng:areq] Argument 'GreetingController' is not a function, got undefined

I've explored various solutions on Stack Overflow to no avail.

The snippet of my Angular code saved as app.js is as follows:

var app = angular.module('ABC',['ngRoute', 'ui.bootstrap']).
controller('GreetingController', ['$scope', function($scope) {
    $scope.greeting = 'Hola!';
}]);


app.config(function($interpolateProvider, $routeProvider){
    //$interpolateProvider.startSymbol('[[');
    //$interpolateProvider.endSymbol(']]');

    $routeProvider.when('/about', {
            templateUrl: 'views/about.html'
    }).when('/login',{
            templateUrl: '/views/login/login.html'
    }).otherwise({
            redirectTo: '/about'
    });
});

And here's my HTML snippet:

{% load staticfiles %}
<!DOCTYPE html>
<html>
    <head>
            <link rel="stylesheet" href="/assets/js/bower_components/bootstrap/dist/css/bootstrap.css">
            <link rel="stylesheet" href="/assets/style/main.css">
            <link rel="stylesheet" href="/assets/style/container.css">
            <link rel="stylesheet" href="/assets/style/button.css">

            <script src="/assets/js/bower_components/jquery/dist/jquery.js"></script>
            <script src="/assets/js/bower_components/bootstrap/dist/js/bootstrap.js"></script>
            <script src="/assets/js/bower_components/angular/angular.js"></script>
            <script src="/assets/js/bower_components/angular-route/angular-route.js"></script>
            <script src="/assets/js/bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
            <script src="/app.js"></script>
            <script src="/views/login/login.js"></script>
    </head>

    <body ng-app="ABC">
            <div class="container-fluid">
                    <nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
                            <ul class="nav navbar-nav">
                                    <li><a href="#home">Home</a></li>
                                    <li><a href="#login">Login</a></li>
                                    <li><a href="#signup">Sign Up</a></li>
                            </ul>
                    </nav>
            </div>

            <div id="wrap"  ng-controller="GreetingController">
                    <div class="container-fluid" id="main">
                            <ng-view></ng-view>
                    </div>
            </div>

     </body>
</html>

I would greatly appreciate any help in resolving this issue.

Here is the link to the source code.

Thank you in advance.

Answer №1

Modified a few elements and added the necessary components like bootstrap, etc. The URL for the file in routeProvider has also been updated. Take a look at the Plunker for more details.

HTML :

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7f70796b727f6c30746d5e2f302a3066">[email protected]</a>" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
    <script src="https://code.angularjs.org/1.4.8/angular-route.min.js"></script>
    <script src="app.js"></script>
  </head> 

  <body>
            <div class="container-fluid">
                    <nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
                            <ul class="nav navbar-nav">
                                    <li><a href="#home">Home</a></li>
                                    <li><a href="#login">Login</a></li>
                                    <li><a href="#signup">Sign Up</a></li>
                            </ul>
                    </nav>
            </div>

            <div id="wrap"  ng-controller="GreetingController">
                    <div class="container-fluid" id="main">
                            <ng-view></ng-view>
                    </div>
            </div>

     </body>

</html>

Angular code :

var app = angular.module('plunker', ['ngRoute']);

app.controller('GreetingController', function($scope) {
  $scope.greeting = 'Hola!';
});

app.config(['$routeProvider', function($routeProvider) {
    //$interpolateProvider.startSymbol('[[');
    //$interpolateProvider.endSymbol(']]');

    $routeProvider.when('/about', {
            templateUrl: 'about.html'
    }).when('/login',{
            templateUrl: 'login.html'
    }).otherwise({
            redirectTo: '/about'
    });
}]);   

Access the live demo on this Plunker link: http://plnkr.co/edit/sHvAOGD2zZVrWMb4NSr8

Answer №2

Your code isn't executing properly. It might help to move your

<script src="/app.js"></script>
tag to the bottom of the HTML file so that it runs after the DOM elements have been rendered.

{% load staticfiles %}
<!DOCTYPE html>
<html>
    <head>
      <link rel="stylesheet" href="/assets/js/bower_components/bootstrap/dist/css/bootstrap.css">
      <link rel="stylesheet" href="/assets/style/main.css">
      <link rel="stylesheet" href="/assets/style/container.css">
      <link rel="stylesheet" href="/assets/style/button.css">

      <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
      <script src="/assets/js/bower_components/bootstrap/dist/js/bootstrap.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.js"></script>
    </head>

    <body ng-app="ABC">
      <div class="container-fluid">
        <nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
          <ul class="nav navbar-nav">
            <li><a href="#home">Home</a></li>
            <li><a href="#login">Login</a></li>
            <li><a href="#signup">Sign Up</a></li>
          </ul>
        </nav>
      </div>

      <div id="wrap"  ng-controller="GreetingController">
        <div class="container-fluid" id="main">
          <ng-view></ng-view>
        </div>
      </div>

     </body>
</html>

<script>
var app = angular.module('ABC',['ngRoute', 'ui.bootstrap']);

app.controller('GreetingController', ['$scope', function($scope) {
    $scope.greeting = 'Hola!';
}]);


app.config(function($interpolateProvider, $routeProvider){
    //$interpolateProvider.startSymbol('[[');
    //$interpolateProvider.endSymbol(']]');

    $routeProvider.when('/about', {
      templateUrl: 'views/about.html'
    }).when('/login',{
      templateUrl: '/views/login/login.html'
    }).otherwise({
      redirectTo: '/about'
    });
});
</script>

<!--script src="/app.js"></script-->
<script src="/views/login/login.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize dynamic global variables in React that are provided during runtime, making them unpredictable during the build process

I am currently developing an application for Overwolf, which you can find more information about at For this platform, applications are built using html/js and run in the Overwolf Browser. The browser provides access to the Overwolf API through a global v ...

JavaScript's prototypical inheritance model allows objects to inherit properties and

Exploring javascript's prototypical inheritance and object-oriented programming is new to me. I attempted to create a base object called Account and then inherit the CheckingAccount from it. Below is my code snippet. function Account(fName, lName) { ...

Having trouble obtaining the serialized Array from a Kendo UI Form

I am working on a basic form that consists of one input field and a button. Whenever the button is clicked, I attempt to retrieve the form data using the following code: var fData = $("#test").serializeArray(); Unfortunately, I am facing an issue where I ...

Using a dynamic HTML interface, select from a vast array of over 50,000 values by leveraging the power

I am working on a project that includes a multiselect option with over 50,000 records. We are using AJAX to fetch data from the server based on user searches, which works fine. However, when a user tries to select all records by clicking the "check all" ...

Can an HTML DOM object be converted to a JSON string using JSON.stringify in JavaScript?

Trying to fetch an external HTML file and convert its body content into a string has been giving me unexpected results. Is there a way to achieve this successfully? var xhr = new XMLHttpRequest(); function loadFile(){ xhr.open("GET", 'index.html ...

Tips on concealing modal popup when the page refreshes successfully

I implemented a progress modal popup to show progress for page reloads. This script is set to the master page and injects the progress modal popup when the form submit event is fired: <script type="text/javascript"> function ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

How can I turn off Angular Grid's virtualization feature, where Angular generates div elements for the grid based on height and width positions?

Currently, I am working with an Angular grid (ag-grid) that dynamically creates div elements in the DOM to display data as the user scrolls or views different sections. As part of my testing process using Selenium WebDriver, I need to retrieve this data fr ...

"Troubleshooting: Why is my Bootstrap modal window only printing a

I've encountered an issue with printing the content of a Bootstrap modal window. Previously, the code I had was working fine but now it seems to be malfunctioning. Content is dynamically added to the form using appendChild() in a separate function. Ho ...

Create a regular expression in Javascript that only matches strings that do not contain any periods

Struggling with setting up an express route for localhost:3000/test and utilizing regex to handle specific URL patterns. Need assistance combining regex with Express params functionality. router.get('/test/:path[\s^.]*$', function () { ...

Deduce the generic types of conditional return based on object property

My goal is to determine the generic type of Model for each property. Currently, everything is displaying as unknown[] instead of the desired types outlined in the comments below. playground class Model<T> { x?: T } type ArgumentType<T> = T ...

Encountering CORS Error while trying to access Guest App in Virtualbox using Vue, Express, and Axios

I encountered an issue while trying to access my Vue app in Virtualbox from the host, both running on Linux Mint 20. Although I can now reach the login page from my host, I am consistently faced with a CORS error during login attempts: Cross-Origin Request ...

How do I extract elements from an array?

I'm working with an array of objects: var aoo = [{},{},{},....{},{},{}]; I am looking for an efficient function that can retrieve elements from index n to m. For example: var getEl = function(from, to) { ... return array } What is the best way to ...

What could be causing my Node application to give a 404 error when making a POST request?

I'm at a loss trying to debug my app for what seems like a simple error, but I just can't locate it. Here is an overview of how my Express app is structured. My routes are set up as follows: var routes = require('./routes'); ... app.g ...

AngularJS and jQuery UI are two popular front-end frameworks that

I've been attempting to integrate angularJS and jquery-ui for the past few hours without any success. I have put together a simple example on fiddle: Check out my Fiddle example var myApp = angular.module('myApp', ['ui']); The i ...

The significance of 'this' in an Angular controller

Forgive me for what may seem like a silly question, but I believe it will help clarify my understanding. Let's dive into JavaScript: var firstName = "Peter", lastName = "Ally"; function showFullName () { // The "this" inside this func ...

The attempt to decode the string from POST using json_decode failed due to a hang-up in

I'm currently learning about PHP, JSON, and JavaScript. I am facing an issue with using the json_decode function in PHP after receiving a JSON string from JavaScript. I am able to save the JSON string to a file without any problem, but when I try to ...

What is the best way to identify onKeyUp events in AngularJS?

Is there a way to detect when a user releases a key in AngularJS? I have been searching for an 'ngOnkeyup' directive, similar to ngChange, without any luck. If this specific directive doesn't exist, is there a simple method to trigger a co ...

Is JQuery the ultimate solution for creating a dynamic multi-language website?

Embarking on a new project that requires support for multiple languages. My plan is to create a jQuery/AJAX based application with all the code in jQuery, simply calling JSONs for data. What would be the most effective approach for implementing multi-lan ...

How can I use jQuery to target and modify multiple elements simultaneously

I've been struggling for the past couple of hours trying to use prop to change the values of two items in a button. One item updates successfully, but the other one doesn't and I can't figure out why. Here is the HTML: <input type=&apos ...