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

What is the best way to transfer a base64 image string obtained from pouchdb to a different api as a file?

I have stored an image in base64 format within pouchdb, and I am now looking to upload the same file to a specific API on the server. Is there a way for me to extract the file path or convert the base64 image back into a regular file so that it can be acc ...

Refreshing the access token in Linkedin's oauth does not result in extending the expiration time

Currently, I am implementing a strategy to renew Linkedin OAuth2 access tokens. When starting the OAuth process in the browser, the dialogue is skipped and a new code is generated. This code is then used to acquire a fresh access_token that differs from t ...

What could be causing the calendar icon to not display in the table cell?

I've been working with the Bootstrap date picker inside a table, and I'm having trouble getting the date picker icon to display using the fas fa-calendar-alt css class. <td> <input type="text" id="expirydate{{$index}} ...

Having difficulty adding items to the shopping cart

While working on a simple checkout system using django and react, I encountered an issue. Upon clicking the add to cart button, instead of adding the item to the cart as intended, I receive a 404 page not found error. I suspect that the problem may lie in ...

Tips for transitioning from GWT to AngularJS

Since the development plugin has been discontinued, GWT development feels less enjoyable. Each small modification results in long recompilations and debugging a mix of Java and Javascript in the browser. I'm considering transitioning to AngularJS. Ca ...

"Interactive elements like dropdown menus and clickable buttons that transform the content of an HTML

My webpage includes three buttons that, when clicked, reveal hidden div elements containing forms. Here is the HTML code for the buttons: <button id="edituser" type="submit" onclick="toggle_visibility('c');" style="border:0;width:100px;margin ...

Frequently refreshing a page to display the most recent information without needing to reload the entire

I am seeking a solution for updating the comments section on my website live or every 30 seconds. The comments are fetched from a MySQL database using PHP: <?php $link = mysql_connect('localhost', 'root', ''); ...

What is the best way to ensure that the maximum price is mandatory when entering a minimum price?

Essentially, the process works like this: if a person inputs a minimum price but forgets to input a maximum price, then presses search, it will not perform the search and a message will appear next to the max price field reminding them to enter a maximum ...

Generate an array of identifiers from a pre-existing array

I am facing a challenge where I need to create an array of IDs for each element in an existing array whose size is unknown. The twist here is that every set of four elements should have the same ID. As an illustration, if the original array (let's ca ...

Ensuring that a canvas remains centered and completely visible within its parent element while zooming in React

Currently, I am developing a straightforward PowerPoint creator using React. In this project, I have integrated a zoom feature for a canvas element. The principle is that the canvas should resize based on a scale factor. However, there seems to be an issue ...

When attempting to convert large objects into JSON using JSON.stringify, a RangeError is thrown due to

Trying to convert a massive JavaScript Object into a string using JSON.stringify in my Node.js application. The objects are extremely large (tens of mega bytes) and do not contain any functions. I need to save the serialized objects to a file. However, I a ...

What is the best way to handle multiple promises when loading a state in Angular?

When loading the /home state, I need to retrieve all users from the database in order to customize the home controller and view based on the logged-in user. Currently, in the :resolve section of the state configuration, I am fetching all 'posts' ...

Attempting to generate a jwt results in an undefined value

Currently in the process of mastering Node.js and I am in the midst of transitioning my existing API from Python to Node. My main challenge lies in the creation of a jwt token for authentication purposes with a third-party API. However, it seems I'm e ...

What could be causing the remaining part of the template to not render when using an Angular directive?

After adding my custom directive to a template on an existing page, I noticed that only the directive was rendering and the rest of the template was not showing up as expected. Even though the controller seemed to have executed based on console logs and B ...

Postponing the loading of a controller until the model is fully loaded without relying on routes

One way to delay the changing of routes until the model is fully loaded is by using a resolve object. To learn more, visit: Delay changing routes until model loaded Is there a similar approach that can be taken for controllers without involving routes? ...

Determining the client web app version in HTTP requests

We frequently update our single page application, but sometimes an older version with a bug can still be in use. It would be helpful if the client could include a version identifier with requests to let us know which code base is being used. Are there est ...

How to Display a Modal Window Automatically Using Bootstrap.js?

Recently, I've become interested in using the Bootstrap library by Twitter for my simple web page. My goal is to have a modal window automatically display after the page loads. If anyone has any tips on how to achieve this, I would greatly appreciate ...

Vue Websockets twofold

I am experiencing some issues with Laravel/Echo websockets and Vue.js integration. I have set up everything as required, and it works, but not quite as expected. The problem arises when I refresh the page and send a request - it displays fine. However, if ...

What is the best way to transmit two variables in a single message with Socket.io?

My goal is to send both the clientid and username through the socket communication. client.userid = userid; client.username = username; client.emit('onconnected', { id: client.userid, name: client.username }); I attempted this approach, how ...

Utilize parent function employing component

Is it possible to add a click listener to a component that triggers a method in the parent template using Vue? <template> <custom-element @click="someMethod"></custom-element> </template> <script> export default { ...