The AngularJS beginner routing application is malfunctioning and needs fixing

I've been diving into Angular JS but hit a roadblock with a basic angular routing program. I need some guidance on what's going wrong.

If you want to check out the complete project code, visit my GitHub repository: https://github.com/ashpratap007/ngRoute

The structure of my files looks like the image below:

https://i.sstatic.net/dsWkm.png

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <link rel="stylesheet" href="styles.css">
    <script type="text/javascript" src="script.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <!-- Angular Route -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-resource.js" type="text/javascript"></script>
</head>

<body ng-app='myApp'>
    <div class="container-fluid row">
        <div class="col-md-12 text-center " style="background-color : #3b5998 ; height: 80px">
            <h1 style="font-family: cursive ; color: white">Store Products List</h1>
        </div>
    </div>
    <nav class="navbar navbar-inverse" style="margin-bottom : 0px">
        <div class="container-fluid">
            <div class="collapse navbar-collapse" id="myNavbar">
                <ul class="nav navbar-nav">
                    <li><a ng-href="#">Home</a></li>
                    <li class="active"><a ng-href="#">Products</a></li>
                    <li><a ng-href="#">Spare1</a></li>
                    <li><a ng-href="#">Spare2</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container-fluid  row" style="height:100% ">
        <div class="row content">
            <div class="col-sm-2 sidenav" style="height: 100%; background-color:#dfe3ee; ">
                <p align="middle"><a href="#/Home">Home</a></p>
                <hr>
                <p align="middle"><a href="#/Products">Products</a></p>
                <hr>
                <p align="middle"><a href="#/Categories">Categories</a></p>
                <hr>
                <p align="middle"><a href="#Link4">Link4</a></p>
                <hr>
                <p align="middle"><a href="#Link4">Link4</a></p>
                <hr>
                <p align="middle"><a href="#Link4">Link4</a></p>
                <hr>
            </div>
            <div class="col-sm-8" style="margin-top : 10px; background-color : #f7f7f7;">
                <div ng-view></div>
            </div>
            <div class="col-sm-2 sidenav" style="height: 100%; background-color: #dfe3ee ">
                <div class="well well-lg">
                    <p>ADS</p>
                </div>
                <div class="well well-lg">
                    <p>ADS</p>
                </div>
            </div>
        </div>

    </div>
    <div class="container-fluid row">
        <div class=" col-md-12 text-center " style="background-color : #3b5998 ; height: 60px">
            <h5 style="font-family: Helvetica ; color: white">CITY GENERAL STORE</h5>
        </div>
    </div>

</body>

</html>

script.js

var app = angular.module("myApp", ["ngRoute"]);
app.config(['$locationProvider',function ($routeProvider, $locationProvider) {
    $locationProvider.hashPrefix('');
    $routeProvider
        .when("/home", {
            templateUrl: "pages/home.html",
            controller: "homeController"
        })
        .when("/Products", {
            templateUrl: "pages/addProduct.html",
            controller: "addProduct"
        })
        .when("/Categories", {
            templateUrl: "pages/categories.html",
            controller: "Categories"
        }).otherwise({
            redirectTo: '/'
        });
}]);
app.controller("addProduct", function ($scope) {
    $scope.productList = [{
        power: false,
        productName: 'Soap',
        productQuantity: 5,
        productPrice: 10
    }];

    $scope.addNew = function () {
        $scope.productList.push({
            power: false,
            productName: $scope.productName,
            productQuantity: $scope.productQuantity,
            productPrice: $scope.productPrice
        });
        $scope.productName = "";
        $scope.productQuantity = "";
        $scope.productPrice = "";
    };

    $scope.removeProduct = function () {
        var oldList = $scope.productList;
        $scope.productList = [];
        angular.forEach(oldList, function (x) {
            if (!x.power) $scope.productList.push(x);
        });
    };

});
app.controller("home", function ($scope) {
    $scope.message = "THE CITY GENERAL STORE";
});
app.controller("Categories", function ($scope) {
    $scope.categories = ["Packaged Food Items", "Fruits",   "Vegetables","Dairy Products", "Plasticware","Staples","Freezed Products"];
});

addProduct.html

<div>
    <form>
        <div class="row">
            <div class="col-sm-12" style="margin-bottom:30px ; ">
                <input style="border-color : #d8dfea" class="btn col-sm-3" type="text" ng-model="productName" placeholder="Product Name" required>
                <input style="border-color : #d8dfea" class="btn col-sm-3" type="number" ng-model="productQuantity" placeholder="Product Quantity" required>
                <input style="border-color : #d8dfea" class="btn btn col-sm-3" type="number" ng-model="productPrice" placeholder="Product Price" required>
                <button class="btn btn-primary btn col-sm-3" type="submit" ng-click="addNew()">Add New Product</button>
            </div>
            <div class="col-sm-12" style="margin-top:30px ">
                <input class="btn col-sm-12" style="border-color : #d8dfea" type="text" ng-model="test" placeholder="Search Product">
            </div>
        </div>
    </form>
    <div style="height: 300px;">
        <table class="table table-hover table-striped" style="margin-top:10px ;background-color : #ffffff;" border=2>
            <tr style="border-color : black; ">
                <th>Checkbox</th>
                <th>Product Name</th>
                <th>Product Quantity</th>
                <th>Product Price</th>
            </tr>
            <tr ng-repeat="p in productList | filter: test">
                <td><input class="checkbox" type="checkbox" ng-model="p.power"></td>
                <td ng-bind="p.productName"></td>
                <td ng-bind="p.productQuantity"></td>
                <td ng-bind="p.productPrice"></td>
            </tr>
            <tr>
                <td colspan=4><button class="btn btn-danger" style="width:100%" ng-click="removeProduct()">Remove Selected Items</button></td>
            </tr>
        </table>
    </div>
</div>

Other HTML files included are purely for testing purposes and may not be directly relevant to this query.

Answer №1

To improve the performance, ensure that you load the references in the correct order. It is important that angular-route and angular are loaded before the script.js file.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>  
<!-- Angular Route -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-resource.js" type="text/javascript"></script>
<script type="text/javascript" src="script.js"></script>

Remember to configure your app using the following code for app.config:

app.config(['$routeProvider','$locationProvider',function ($routeProvider, $locationProvider) {

DEMO

Answer №2

(Shared by the original poster).

Resolution: I discovered that I had mistakenly arranged the CDN links in the wrong order. Specifically, I was importing the Angular route before the Angular CDN itself. By making adjustments to the import sequence as well as modifying the content below, I successfully resolved the issue.

    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Angular Route -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-resource.js" type="text/javascript"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="styles.css">
</head>

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

Organize chat messages based on date using JavaScript

I came across a similar query, but it didn't resolve my issue. Check this out: How can I group items in an array based on date? My goal is to organize these chat messages by date after fetching them from the database using AJAX. The console displays ...

Strategies for optimizing PPI and DPI in responsive design

Imagine having two monitors: one 15.5 inches with a resolution of 1920 x 1080 and the other 24 inches with the same resolution. The first monitor has a pixel density of around 72 PPI, while the second has around 90 PPI. If I apply a media query in CSS for ...

Handling Promises in Angular 1 App using Typescript ES6/2015

Currently, I am working on an Angular 1.5 application that utilizes Typescript. My main concern is finding the most efficient way to handle ng.IPromise compared to Promise (ES6 promise). Personally, I would prefer to solely deal with the ES6 Promise type. ...

Sorting JSON data using JQuery Ajax

I've encountered an issue with sorting JSON data. Here is the JSON data I'm working with: [ { nom: "TERRES LATINES", numero: "0473343687", image: "http://s604712774.onlinehome.fr/bonapp/api/wp-content/uploads/2016/12 ...

The ability to rate the product in Livewire:Laravel is exclusively reserved for buyers

I have integrated the Livewire rating system into my Laravel e-commerce platform. Currently, all users can rate and comment on any product. However, I want to restrict this privilege to only buyers. ProductRatings.php class ProductRatings extends Componen ...

Focus on the iPad3 model specifically, excluding the iPad4

I am looking to apply specific CSS that works on iPad 3, but not on iPad 4, or vice versa. Currently, I am able to target both versions by using the following code: @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and ( m ...

Combining two objects in AngularJS to create a new merged object

Is there a way to merge object1 and object2 into object3, updating values corresponding to matching keys while ignoring unmatching keys? var object1 = { "pii" : "val1", "loc" : "val2" } var object2 = { "rrb" : "val3", "voc" : "val4" } var obje ...

Transfering data to Handlebars while rendering a template

Is there a method to pass a variable to a handlebars template during its rendering process? Below is the template in question: <script id="listingTopics" type="text/x-handlebars-template"> {{#each this}} <div class="wrapper_individual_listing ...

The setInterval function continues to execute endlessly even after each interval is three months

I need to remove all expired OTP records every three months. In the file /sheduled-tasks/OTPRecords.js const prisma = require("../services/prisma"); const THREE_MONTHS = 1000 * 60 * 60 * 24 * 90; async function deleteExpiredOTPRecords() { ...

The fetch function in my Node.js code is failing to properly display JSON data from a file on the console

I am currently exploring node.js and working on building a web server to fetch a list of team members from a JSON file in the 'json' folder. However, I am encountering an issue with my fetch function as it is not displaying the data on the consol ...

Clickable Href in jquery autocomplete

These are the codes I am currently using: <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.1 ...

Minimize the loading time of your Meteor application

During the development of our meteor application (with angular), we have observed that the initial load times (without cache) are quite slow, taking approximately 10 seconds. The modules.js file seems to be the main issue, as it contains all our node_modul ...

Cloning jQuery with varied PHP array value

So, I have the given PHP values: PHP: <?php $names = array("Mike","Sean","Steve"); ?> <script type="text/javascript"> var name_data = <?php echo json_encode($names); ?>; </script> <div class="container"> <div cl ...

Having trouble with Angular 2 Routing and loading components?

I am facing an issue with Angular 2 where it is searching for my component in app/app/aboutus.component, but I cannot pinpoint the source of the problem. Here is my app.component.ts code: import { Component } from '@angular/core'; import { ROUT ...

Problem with Vue.js dropdown functionality in Internet Explorer

After developing a form using Vue.js to allow users to save and return to their answers, I encountered an issue in Internet Explorer. When the page loads, the dropdown menu tied to a computed property does not display the previously selected answer as expe ...

What is preventing me from utilizing a dynamic import while working with Nuxt3?

I'm currently working on developing a component that allows me to dynamically import icons based on the provided icon name using unplugin-icons. However, I'm facing an issue where dynamic imports don't seem to work when the import path is dy ...

Unexpected JSONP Parsing Issue Despite Correct JSON Data

I've implemented a Cross Domain AJAX request using JSONP, and it's working fine with CORS. However, I'm facing an issue with JSONP. I've checked other threads but couldn't find a solution for my case. Below is the code snippet: ...

Creating a node.js function that can be used with OracleDB

I'm currently delving into learning nodeJS, but I'm facing a roadblock and can't seem to figure out what's causing the issue. Even the Debugger isn't providing much help. Any assistance or guidance would be greatly appreciated. The ...

Integrating jQuery Tooltip with Mouseover Event

I'm currently involved in creating a map of MIT projects for an architectural firm, and facing the challenge of maintaining the red dots mouseover state even when the mouse hovers over the tooltips that appear. Presently, the mouseover effect turns of ...

Create a dynamic animation effect for the placeholder in an input field that triggers when the user starts typing

Have you ever come across interactive demos like this one? I've noticed that most examples involve creating a new element. parent().append("<span>" + $input.attr('placeholder') + "</span>"); Is there a way to make the placehol ...