Angular's Routeprovider: Navigating Your Way through the

I am a newcomer to Angular and I encountered an issue while trying to implement routeProvider in my app. The error message that keeps appearing is:

Uncaught Error: [$injector:modulerr] Failed to create module 'app' due to: Error: [$injector:nomod] Module 'app' is not available! You may have misspelled the module name or forgotten to load it properly. When registering a module, make sure to specify dependencies as the second argument.

This is how my index.html is structured:

<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Title</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.min.js" type="text/javascript"></script>
    <script src="js/app.js" type="text/javascript"></script>
</head>
<body>
    <div ng-view></div>
</body>
</html>

The contents of my app.js are as follows:

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

myApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'components/splash.html',
        controller: 'segmentListCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
});

myApp.controller('segmentListCtrl', ['$scope', '$http', function ($scope, $http){
    $http.get('data/segments.json').success(function (data){
        $scope.segmentList = data;
    });
}]);

Below is the content of the splash.html which serves as the template for my routing:

<div id="splash" class="row" ng-controller="segmentListCtrl">
    <div class="columns segment" ng-repeat="segment in segmentList">
        <a href="#">
            <h2>{{segment.title}}</h2>
            <h5>{{segment.sub}}</h5>
        </a>
    </div>
</div>

Everything was functioning smoothly before attempting to integrate ng-route. However, after breaking it down into parts, I started encountering this error which I can't seem to resolve. Appreciate any guidance provided on this matter!

Answer №1

You have a little oversight in your myApp.config file - you forgot to include the closing brace ] at the end.

Since you're new to Angular, it's worth noting that when specifying the controller in your .when('/' ....), it is equivalent to adding an ng-controller directive at the top of your template. Therefore, there is no need to redeclare it within the template itself.

For example:

<div id="splash" class="row" ng-controller="segmentListCtrl">

can simply be written as

<div id="splash" class="row">

Answer №2

It seems like there is a syntax error in your app config declaration:

myApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'components/splash.html',
        controller: 'segmentListCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
});

It should actually be:

myApp.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'components/splash.html',
        controller: 'segmentListCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
}]);

As you can see, the final ']' is missing. Let me know if this fixes the issue for you. Thank you!

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

When utilizing a wrapped v-text-field, it triggers warning messages and fails to update the data properly

Recently delving into the world of vue.js, I've been experimenting with creating a form using Vue, vuetify, and vuelidate. Oddly enough, when I don't wrap the v-text-field there are no issues, but as soon as I try to encapsulate it within compone ...

I am looking to fetch information from a different Firestore collection by looping through data using a forEach method within an onSnapshot function

I'm struggling to grasp the concept of rendering data from Firestore in my project. I've searched extensively but haven't been able to find a solution that fits my requirements. Background Information In my Firestore database, I have collec ...

What is the procedure for converting a JSON object with type and value into a map in Scala?

Currently, I am facing the challenge of deserializing JSON that has the following structure: { "states": { "Position" : { "x": 1, "y": 2, "z": 3 }, "Timestamp" : { "value" : 123 } } } The names Position and Timestamp represent the classes that ...

Having difficulty with Mongoose in MongoDB when trying to retrieve an array of strings that match existing collections in the database

I've designed a helper function that is supposed to generate a promise containing an array of strings that represent all the names of Collections currently stored in my database. After conducting console logs, I confirmed that my connection to the da ...

Customizing Material UI Select for background and focus colors

I am looking to customize the appearance of the select component by changing the background color to "grey", as well as adjusting the label and border colors from blue to a different color when clicking on the select box. Can anyone assist me with this? B ...

Unable to Pass POST Parameters Through Volley Request

I am facing an issue with the code implemented on my Android client: int method = Request.Method.POST; JSONObject params = new JSONObject(); try { params.put("data", userJson); } catch (JSONException e) { LogSystem.e(tag, ...

Problem parsing Dart: encountering issue with serializable and missing build value

Hello everyone, [edited: trying to run this code in dartpad import 'dart:convert'; void main() { const String _json = '{"myListInt": [1]}'; final Map<String, dynamic> _map = jsonDecode(_json); final List<int> _list ...

Looking to receive child events within a Vue 3 setup()?

Looking to convert the code below to use Vue 3 composition API. I am trying to listen for an event from a child component in a parent component that utilizes the render function. In Vue 3, $on has been removed and I'm unsure of how to replace it in t ...

Using JS regular expressions to only select anchor (a) tags with specific attributes

When trying to select a link tag (a) with a specific data-attr, I encountered an issue. I currently use the following regex pattern: /<a.*?data-extra-url=".*?<\/a>/g. However, I noticed that this selection goes wrong when there are no line br ...

Errors related to missing RxJS operators are occurring in the browser, but are not showing up in Visual Studio

Recently, I encountered a peculiar problem with my Angular4 project, which is managed under Angular-CLI and utilizes the RxJS library. Upon updating the RxJS library to version 5.5.2, the project started experiencing issues with Observable operators. The s ...

Using D3.js for Page Navigation

For my D3 bar charts, I am working with a substantial amount of JSON data obtained from an API. My goal is to display only 10-20 bars at a time. Would it be possible to implement pagination using D3 itself, or should I explore alternative methods (such a ...

Unexpected outcome when returning a map

Encountered a puzzling issue that requires immediate clarification. When I input the following code into my project: this.metadata = json.metadata.map((x) => {return new Metadatum(x);}); console.log(this.metadata[0].value); The output consistently sho ...

Divergence between Angular HTTP get response and network tab display

I've been encountering an intermittent issue where, when I use the Angular HTTP service to make a call, some values are coming back as undefined every 10th time or so when I refresh the page. It's strange because the network tab confirms that all ...

What is the significance of a listener signaling an asynchronous response with a return of true, only to have the communication channel close before the response could be received?

Currently, I am developing a React application that involves the use of various npm modules. One of these modules is a self-built NPM package called modale-react-rm (available at this link). This package serves as a simple modal component that utilizes the ...

Pulling data from a MySQL database using AJAX and PHP to convert it into a JavaScript variable, resulting in

I am attempting to retrieve MySQL data using PHP, convert it to JSON, and then pass it into my JS variables for Chart.js. The JSON generated by PHP appears correct. However, when trying to access the data within my variables, the console is displaying them ...

Sending a PDF document and JSON data via an AJAX POST request

I'm attempting to send a PDF document along with some JSON data in string format using an AJAX method in jQuery to an ASP.NET WEB API (2). Below are my attempts that are not working: JAVASCRIPT: // Obtaining the authorization token works fine var hea ...

In what way can I indicate parsing "per dataset" using Chart.js?

I'm currently exploring the usage of the yAxisKey option in ChartJS while defining a dataset. However, I'm encountering difficulties in replicating this specific example from the documentation. Despite my efforts to search for issues related to y ...

Using cfajaxproxy in conjunction with URL rewriting capabilities

I have successfully integrated the cfajaxproxy tag, but encountered an issue with the cfc's location in the root directory of my site. When accessing a page within the root directory through a rewritten URL (e.g. mysite.com/one/two/ -> mysite.com/ ...

Steps to trigger a JsonProcessingException with Jackson

Here's an interesting query - I'm looking to enhance the test coverage of my code and have encountered a challenge. Despite coding against a JsonProcessingException, I am unable to create a payload that triggers this exception. It seems that Jack ...

JavaScript Navigation Bar Error: The value of 'undefined' is not recognized as an object

Having just started learning HTML, CSS, and JavaScript, I encountered an error message that reads: TypeError: 'undefined' is not an object. Despite my best efforts to troubleshoot the issue, I have been unable to resolve it. Is there anyone who c ...