Exploring the latest routes in Angular JS to create a unique and dynamic partial layout

/// <reference path="Scripts/angular.js" />
var myApp = angular.module("myModule", ["ngRoute"])
                    .config(function ($routeProvider) {
                        $routeProvider
                            .when("/home", {
                                templateUrl: "homeHTML.html",
                                controller: "homeController"
                            })
                            .when("/courses", {
                                templateUrl: "coursesHTML.html",
                                controller: "coursesController"
                            })
                            .when("/student", {
                                templateUrl: "studentHTML.html",
                                controller: "studentController"
                            })
                    })
                    .controller("homeController", function ($scope) {
                        $scope.message = "Home";
                    })
                    .controller("coursesController", function ($scope) {
                        $scope.message = "Courses";
                    })
                    .controller("studentController", function ($scope) {
                        $scope.message = "Student";
                    });
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myModule">
<head>
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Script.js"></script>
    <title></title>
</head>
<body>
    <table>
        <tr>
            <td colspan="2" style="width:600px; height:100px; background-color:#b4b4b4; text-align:center">
                This is the header area
            </td>
        </tr>
        <tr>
            <td style="width:200px; height:400px; background-color:#ff0000">
                <ul>
                    <li><a href="#/home">Home</a></li>
                    <li><a href="#/courses">Courses</a></li>
                    <li><a href="#/student">Student</a></li>
                </ul>
                
            </td>
            <td style="width:400px; height:400px; background-color:#ff6a00">
                <ng-view></ng-view>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="width:500px; height:100px; background-color:#b4b4b4; text-align:center">
                This is a Footer Area
            </td>
        </tr>
    </table>

</body>
</html>

homeHTML, studentHTML, coursesHTML all contain the same content as shown below.

<h1>{{message}}</h1>
<div>
    <p>Hi this is home partial view</p>
</div>

In the snippet section, there was an error:

Error:{
  "message": "Uncaught ReferenceError: angular is not defined",
  "filename": "http://stacksnippets.net/js",
  "lineno": 51,
  "colno": 13
}

The issue here is that I am seeing the view in the snippet without any errors appearing in the browser. However, when clicking on the links for Home, Courses, and Student, nothing is displayed. Please assist in identifying what went wrong in this code.

Answer №1

Hey everyone, guess what? I just discovered the solution!

/// <reference path="Scripts/angular.min.js" />
var myApp = angular.module("myModule", ["ngRoute"])
                    .config(function ($routeProvider, $locationProvider) {
                        $locationProvider.hashPrefix('');
                        $routeProvider
                            .when("/home", {
                                templateUrl: "homeHTML.html",
                                controller: "homeController"
                            })
                            .when("/courses", {
                                templateUrl: "coursesHTML.html",
                                controller: "coursesController"
                            })
                            .when("/student", {
                                templateUrl: "studentHTML.html",
                                controller: "studentController"
                            })
                    })
                    .controller("homeController", function ($scope) {
                        $scope.message = "Home";
                    })
                    .controller("coursesController", function ($scope) {
                        $scope.message = "Courses";
                    })
                    .controller("studentController", function ($scope) {
                        $scope.message = "Student";
                    });

I've included $locationProvider. More details on why will follow shortly.

Answer №2

To start, initiate the angular module in either your html or body tag and ensure that your angular version matches the angular-router version. It is also crucial to define the respective controllers in homeHTML.html, coursesHTML.html, and studentHTML.html.

Here is an example from homeHTML.html:

<div ng-controller="homeController">

    <h1>{{ message }}</h1>

</div>

This implementation has been successful for me.

For more information, you can visit: https://plnkr.co/edit/cZzk8EKBXqRuoy6oYhuF?p=preview

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

Is there a way to display the input value from an on-screen keyboard in an Angular application?

https://i.sstatic.net/j76vM.pnghttps://i.sstatic.net/EQPZO.png I have included my code and output snippet below. Is there a better way to display the input value when clicking on the virtual keyboard? ...

Encountering issues while attempting to execute node-sass using npm

Currently, I'm attempting to execute node-sass using npm. Displayed below is my package.json: { "name": "my-project", "version": "1.0.0", "description": "Website", "main": "index.js", "scripts": { "sass": "node-sass -w scss/ -o dist ...

leveraging hooks in NextJS app router for static page generation

How can I make an action take effect on page-load for the app router equivalent of statically generated pages from static paths in NextJS? Everything is working fine with my page generation: // app/layout.js import Providers from '@/app/Providers&apo ...

Making a POST request to Keycloak from Postman successfully retrieves the access token, however, using AXIOS in a NodeJs environment does not yield

I'm having trouble fetching the access token from Keycloak using Axios in NodeJs. When I make the call, it fails with a 'connect ECONNREFUSED ::1:8080' error. However, the same call is successful when made through Postman. I can't seem ...

Utilizing a JavaScript function to toggle the Bootstrap dropdown without the need for manual clicking

I've configured a Bootstrap dropdown in my site's mini cart that includes a lightbox effect to grey out the background content when the dropdown is activated. Here's the code snippet: $(".dropdown").on('show.bs.dropdown hide.bs.dropdow ...

Javascript - accessing a local file (located in the same directory as the HTML file, not an uploaded file)

I have been attempting to find a solution, but have had no success. Is there a way to read a file using JavaScript or HTML? I have a text file named "sample.txt" with some information in it. This file is located in the same folder as the HTML file that con ...

Stop the web browser from storing the value of the input control in its cache

Many web browsers have a feature that saves the information you enter into certain fields so you don't have to type it again. While this can be convenient for most cases, I am faced with a situation where users must input a unique reference for a new ...

Angular JS: Automatically toggle the visibility of a div on a panel click event

There is a row, panel, and a closed div on load. Clicking on the panel should make the div appear while making the row and panel disappear. Clicking on X should make the panel and row appear again. <script src="https://ajax.googleapis.com/ajax/libs/ ...

I am looking for ways to identify the specific code responsible for causing a JavaScript heap out of memory issue

When I attempt to execute yarn start, I encounter the following error message: Starting the development server... ts-loader: Using [email protected] and C:\DevTools\git\mymoto\tsconfig.json <--- Last few GCs ---> [9076:000 ...

Tips for utilizing an iOS application to transfer an image to a Node.JS server

Hello everyone, I'm facing a challenge where I am attempting to upload an image from my iOS device to a web server that is written in Node.JS. On the web server, I am using formidable and it functions properly when I upload a file via a browser. Howe ...

Set up AngularJS application to save API URLs

In configuring my API endpoint and URL's, I utilize a custom app.config.js file with environment variables: angular.module('api-config', []).constant('ENV', { name: 'My Project Name', apiEndPoint: 'http://SO ...

Having trouble sending a POST request with Python and Node.js - any ideas?

Despite researching numerous articles on this topic, I am still encountering an error. Python snippet: import requests import json url = 'http://127.0.0.1:8080/ay' payload = {'some': 'data'} r = requests.post(url, data=pay ...

The resolve functions in UI-Router are designed to be executed just once, regardless of whether the reload option is enabled

Whenever I run $state.go("main.loadbalancer.readonly"); in Angular ui router, and if main.loadbalancer.readonly has already been activated earlier, my resolve: {} doesn't get executed or evaluated. The resolve simply gets bypassed and I confirmed this ...

The Express middleware is not being utilized

There seems to be an issue with the middleware in the code below that is supposed to create a property req.user if req.session.user exists. Unfortunately, it appears that the middleware is not being triggered, causing 'hihihhiihi' to not be print ...

Having difficulty automatically populating a textarea with the chosen option from a datalist

Is there a way to automatically populate the Address field of a client in a textarea based on the input of the client's name in an input field? I have created a 'for loop' to retrieve a datalist of client names. For the address, I retrieved ...

Vue - Find matching data in an array of objects and render it on the page

I have a Vue.js data array that contains information about counties in the UK. I want to create links between related county pages based on the data in the array. To achieve this, I need to loop through the main county object and compare the items in the ...

Error: The module jacksApp failed to instantiate because of an uncaught error. The reason for this error is that the injector could not find the module jacksApp

I've been delving into Angular recently, but I'm running into some issues with it not loading properly. Despite searching on SO for similar questions, I couldn't find the solution I needed. Can anyone pinpoint why this error is happening? C ...

Exploring the dynamic shift with jQuery .find

Currently, I am in the process of learning jQuery and have come across a simple issue. My goal is to change the text within a div when a link is clicked. The div in question is a duplicate of another div on the page, and my intention is to remove the copy ...

What causes the namespace to shift when utilizing npm for installing a library?

I've been attempting to integrate whammy.js into a project. The initial line in the source code is window.Whammy = (function(){ yet, after running npm i and inspecting node_modules, I discovered global.Whammy = (function(){ https://github.com/anti ...

Using React and Material UI to hide a child element when it is hovered over with the help of withStyles

I am trying to hide the ListItemSecondaryAction element with the class actions when hovering over the ListItem with the class friendsListItem. Despite attempting to use the descendent selector within styles, I have not been able to achieve the desired res ...