What could be causing the routing to fail in this script?

Currently working on a small project to dive into AngularJS, and I'm facing some challenges with getting the routing to function properly. Despite following numerous tutorials, my code doesn't seem to be working as expected. If anyone could lend a hand in pinpointing the issue and helping me resolve it, I would greatly appreciate it.

Below is a snippet from my index file:

<!DOCTYPE html>
    <html ng-app="danApp">
        <head>
            <script src="Scripts/angular.min.js"></script>
            <script src="Scripts/test.js"></script>
        </head>
        <body>
            <div ng-view></div>
        </body>
    </html>

View 1

<h1>View 1</h1>
<br><br><br><br>
<div>
   <input type="text" ng-model="nameText" >
      <ul>
         <li ng-repeat="things in info | filter: nameText | orderBy:'name'">
            Name: {{ things.name }} <br> Age: {{ things.age }} <br>
            City: {{ things.city }}
            <br>
         </li>
      </ul>
</div>

View 2

<h1>View 2</h1>
<br><br><br><br>
<div>
    <input type="text" ng-model="nameText" >
      <ul>
         <li ng-repeat="stuff in parents | filter: nameText | orderBy:'name'">
            Name: {{ stuff.name }} <br> Age: {{ stuff.age }} <br>
            City: {{ stuff.city }}
            <br>
         </li>
      </ul>
</div>

Lastly, here's my module, controllers, and routing code:

"use strict";

 var danApp = angular.module('danApp', []);
               
 danApp.controller("danCtrl", function($scope) {
    $scope.info = [
        {name: 'Daniel', age: '22', city: 'Clarksville'},
        {name: 'Derek', age: '19', city: 'Jacksonville'},
        {name: 'Emily', age: '18', city: 'Erin'},
        {name: 'Denise', age: '27', city: 'Phoenix'}, 
    ];
 });
               
               
 danApp.controller("danCtrl2", function($scope) {
   $scope.parents = [
      {name: 'Lathan', age: '54', city: 'Stewart'},
      {name: 'Candy', age: '54', city: 'Stewart'},
      {name: 'Christine', age: '43', city: 'Erin'}
   ];
 });           
               
               
danApp.config(function ($routeProvider) {
   $routeProvider
      .when('/',
      {
         controller: 'danCtrl',
         templateUrl: 'views/view1.html'
      })
      .when('/view2',
      {
         controller: 'danCtrl2',
         templateUrl: 'views/view2.html'
      })
      .otherwise({redirectTo: '/'});
});

Answer №1

To resolve the issue, make sure to download either angular-route.js or angular-route.min.js from the official Angular website. Once downloaded, add the following script to your index.html file:

<script src="Scripts/angular-route.min.js"></script>

Next, include the module in your Angular application like this:

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

For a live example, you can view it on Plunkr.

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

Transferring an MSAL token to the backend with the help of Axios

I encountered an issue while attempting to send the user ID, which was previously decoded from a JWT token along with user input data. The problem arises when I try to send the data and receive an exception in the backend indicating that the request array ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

What is the method to turn off readonly property for input fields in Laravel?

I'm encountering an issue with my Laravel project. I have a form with an input field that has the readonly attribute set. <input type="text" name="anamFam" id="anamFam" value="{{$aFam->anam_cont}}" readonly> When I click the edit button, I ...

Filtering deeply nested arrays

Hey, I'm working with this interesting array: [ { "Navn": "Long Island Iced Tea", "Nummer": "2", "Glas i ml": "250", "Instruktioner": "", "a": "Hæld is i glasset", "b": "pynt med en skive lime", ...

Unable to submit form upon clicking radio button in .NET Core

Trying to submit a form by clicking on a radio button. The JQuery code used for submitting the form: Form: @for (var item = 0; item < Model.Count(); item++) { <form id="myform" action="xx" controller="xxx" method="post"> ...

Transforming a namespaced function into an asynchronous operation by utilizing setTimeout

Looking for help with making a function that uses namespaces asynchronous. The function is currently being called on the click of a button. var ns = { somemfunc: function (data) { alert("hello"); } } Edit: ...

Secure your website with the latest JWT cookie security measures

After storing a JWT with an expiry date set 30 days ahead, the question arises - is it secure to store this JWT in a cookie? The aim is for the token to persist beyond a single session, much like the "Keep me logged in" feature found on some websites. Se ...

The method by which AngularJS identifies the appropriate property within a return value

In Angular, watchers are utilized to identify property changes and trigger a listener function. An example of a watcher declaration is shown below: $scope.food = "chicken"; scope.$watch( function() { return food; }, function(newValue, oldValue) { ...

Instead of using a string in the createTextNode() function, consider utilizing a variable

I am attempting to use JavaScript to add another list item to an unordered list. I want the list item to display dynamic content based on a pre-existing variable. While I can successfully append a list item by using a string, things go awry when I try to i ...

Exploring the Depths of React Routing: The Power

I'm currently diving into React and trying to figure out how to create dynamic routes similar to partial pages in Angular. Here is my main App component: import React from 'react'; import Header from '../common/Header'; export d ...

Employing ng-repeat within a ui-scope

I'm having trouble getting my ui-view to update dynamically using ng-repeat. I'm not sure if what I want to do is even possible because when I add static objects to intro.html, they display properly. Thank you for any assistance, JS }).st ...

Connecting the value of one input to influence another input

There are two input boxes provided - one for current address and another for permanent address. When the checkbox is clicked, the value of the current address should be displayed in the permanent address box. However, I am facing an issue where when I unc ...

How can a parameter be added to a query string only when there is a value present in a textbox using JavaScript?

Hello everyone, I am looking to add parameters to a URL only if the search text box value is different from the default. I have three search text boxes and I want to include them in the URL. Below is my code snippet: $("#search-btn").click(function (e) { ...

Having trouble running nodemon on my Windows 10 machine

Recently, I started diving into Node.js and managed to run my first node app successfully, albeit without using nodemon. To remedy this, I globally installed nodemon by running npm install -g nodemon, which went smoothly. However, upon executing nodemon in ...

Tips for Accessing Values in a Dynamic Array in Vue.js

ArrayOrdered :[ { name :"PRODUCT 1", price :"20", amount:"10", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 2", price :"50", amount:"20", Total 1:" ", discount : "" , Total 2:" " }, { name :"PRODUCT 3", price :"15.5", amount:"10", Total ...

Set the Checkbox selections by utilizing the array values in the MUI DataGrid

In my MUI Datagrid, I have 2 columns for ID and City Name. There are only 3 cities represented in the table. Within a citizen object like this: const userMockup = { id: 1, name: "Citizen 1", cities: [1, 2], isAlive: true }; The cities ar ...

When using the combination of Cucumber/Capybara with Angular, the test successfully passes with the Selenium driver but does not work with

I've been attempting to conduct feature tests on an Angular application, but I'm experiencing failures when using the poltergeist driver. It appears that the issue stems from the data-binding syntax being interpreted literally. For example, in th ...

Fetch information that was transmitted through an ajax post submission

How can I retrieve JSON formatted data sent using an ajax post request if the keys and number of objects are unknown when using $_POST["name"];? I am currently working on a website that functions as a simple online store where customers can choose items m ...

Unable to access $_SESSION variable when making AJAX request from a different port

After creating a website with PHP on port 80 (index.php) and setting the session variable with the userid upon login, I encountered an issue when clicking on a link that redirected me to port 8080, which is where a JavaScript file containing node.js proces ...

What is the best way to implement conditional hook usage in React?

Having two hooks at my disposal, useLocalStorage and useQuery, I find myself wondering about conditionally utilizing one of them based on an input prop. I'm considering writing the following: const [value, setValue] = localStorage ? useLocalStorage() ...