Issues with basic routing in Angular version 1 are causing problems

I'm encountering an issue while setting up my first Angular app with routing. It seems to be a simple problem but it's not working properly.

Whenever I click on the links, the URL changes.

  1. index.html - file:///C:/Users/me/repos/angularRouteTest/app/index.html#!/
  2. about click - file:///C:/Users/me/repos/angularRouteTest/app/index.html#!/#about

However, the content inside ng-view remains unchanged.

Here's the code snippet:

<!DOCTYPE html>
<html lang="en>
<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="bower_components/angular-route/angular-route.min.js"></script>
    <script src="scripts/app.js"></script>
</head>
<body data-ng-app="swsApp">
    {{1+1}}
    <p><a href="#/">Index</a></p>
    <a href="#about">About</a>
    <div ng-view></div>

</body>

(function() {
"use strict";

var app = angular.module("swsApp", ["ngRoute"]);

app.config(function($routeProvider) {
    $routeProvider
        .when("/", {
 template : "<h1>Main</h1><p>Click on the links to change this content</p>"
        })
        .when("about", {
 template : "<h1>About</h1><p>Click on the links to change this content</p>"
        })
        .otherwise({
            template: "<h1>otherwise</h1>"
        })
})
}());

Does anyone have any idea what might be causing this issue?

Answer №1

Launching an AngularJS app does not require a web server.

If you are facing issues with links in AngularJS, keep in mind that AngularJS uses hashbang URLs. Here's the documentation to help you understand more: https://docs.angularjs.org/guide/$location#hashbang-mode-default-mode-

A hashbang URL signifies that AngularJS will only intercept links with the hashbang #!

All you need to do is add the hashbang to each link in your HTML code, like this:

app.config(function($routeProvider) {
    $routeProvider
        .when("/", {
            template: "<h1>Main</h1><p>Click on the links to change this content</p>"
        })
        .when("/about", {
            template: "<h1>About</h1><p>Click on this content</p>"
        })
        .otherwise({
            template: "<h1>otherwise</h1>"
        })
})
</script>
<body data-ng-app="sws">
    {{1+1}}
    <p><a href="#">Index</a></p>
    <a href="#!/about">About</a>
    <div ng-view></div>

</body>

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

Eliminating the unwanted line breaks that are automatically inserted when integrating JavaScript with HTML code

Hey everyone, I'm new to web development and could really use some assistance. I am currently working on a website that shows the weather forecast for the next four days, using SimpleWeather.js. In my JavaScript, I want to display the High & Low temp ...

What are some strategies to enhance the efficiency of this code and reduce repetition?

Here's an overview of the component in question export default () => { const { currentUser, logout } = useAuth(); const [sideBarOpen, setSideBarOpen] = useState(false); const theme = useTheme(); const classes = useStyles(); const isSmall ...

In what ways can I incorporate Django template tags into my JavaScript code?

I've encountered an issue with implementing my model data into a FullCalendar library template in JavaScript. Despite seeing others do the same successfully, I keep getting errors as the template tags fail to register properly. <script> documen ...

Ways to acquire ttf files from a third-party domain without encountering CORS issues

I am attempting to obtain a .ttf file from an external website for use in my web application. However, when I try the following code: @font-face { font-family: 'font'; src: url('http://xxx.xyz/resources/tipografias/font.ttf') forma ...

Despite being deployed on Vercel, the process.env variables in Nextjs are still not functioning as expected

I'm currently working on a project that involves using 4 api keys that I need to keep hidden: STORYBLOK_API_KEY= EMAILJS_SERVICE_ID= EMAILJS_USER_ID= EMAILJS_TEMPLATE_ID= All of these keys are being accessed using process.env.XXX. What's inte ...

Strange behaviors when using setinterval with classes

Currently working on enhancing a slider functionality, and I have observed that everything is functioning smoothly - function Slider(element) { this.i = 0; this.element = element; var self = this; this.timer = window.setInterval(functi ...

View / Conceal Unordered List depending on link selected

How can I dynamically show or hide UL elements based on the clicked link? Here is a JS Fiddle example: http://jsfiddle.net/zangief007/rj8g0yh4/1/ Below is the HTML code: <ul class="category-1 group"> <li class="name">JOHN Smithe QC</l ...

sending a pair of parameters from two dropdown menus to a controller method in Grails

I am currently working on a project where I have two dropdown lists - one depends on the other for its values. I also have a button where I need to pass the selected values from both dropdowns to a controller function for a query. Does anyone have any adv ...

Positioning of dropdown in Material UI select component

Unfortunately, I am encountering an issue with the Menuprops attribute and cannot seem to adjust the position of the drop-down box. Despite following the instructions from other similar queries, the desired outcome remains unachieved. My goal is to have t ...

Is it possible to have Mobile WebKit store the click position?

I am in the process of developing a unique framework specifically designed for WebKit devices. One of the features I have implemented is a series of list views that activate a 'hover' class when users touch them. Below is the jQuery/JavaScript co ...

Effortless Inertia Scrolling Implemented in Next.js

My search for achieving smooth inertia scrolling in next.js has left me unimpressed with the available solutions, as they either lack performance or come with some drawback. This medium article and sandbox demo provided a choppy experience, While this jav ...

"Exploring the process of looping through a JSON object following an asynchronous retrieval of JSON data using

I am facing an issue while trying to iterate through a JSON object in jQuery after fetching it asynchronously. I have a function called 'listFiles' that uses async to successfully retrieve a file list from a directory (dir) by calling an API endp ...

Unordered calling of functions in JavaScript - is it possible?

I'm currently working on a project that involves extracting data from an SQL database and converting the output of a query (which is a number) into a corresponding color, which is then passed to a JavaScript variable. Essentially, I am using ajax to ...

The form control is missing a specified name attribute, causing an error with the value accessor

<input type="email" class="form-control passname" [(ngModel)]="emailID" name="Passenger Email ID" placeholder="email" required pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$"/> <div class="shake-tool ...

What is the best way to configure Winston with Webpack?

I'm working on an electron application that utilizes node.js and I want to integrate the Winston logging library. After adding Winston to my package.json file, I encountered warnings from the colors.js dependency when running the webpack build command ...

Utilize a dynamic URL within the HTML audio element

Currently, I am working with the use of the audio tag in HTML and AngularJS. I need to specify the source of the audio file I want to play, as shown below. <audio src="http://localhost/audio/221122/play/212223.mp3" audio player controls p ...

Techniques for eliminating single quotes from string arrays and then creating a new array by separating them with commas

I have an array of elements containing IP addresses with single quotes. I need to remove the single quotes and separate each IP address into new values, storing them as strings in another array using JavaScript. let myArray = [ '10.202.10.800,10.202 ...

Passing variable values from .post response to PHP: A guide

I'm currently working on integrating Geocode from the Google Maps API into my WordPress plugin within the wp-admin. I have created a custom post type that includes an input field for the address of a place, and I also have jQuery code set up to watch ...

Leveraging jQuery functions with dynamically generated DOM elements in JavaScript

On my website, I have a button that dynamically generates a dropdown menu, a textfield, and two buttons when clicked. The purpose of the textfield is to track the quantity selected, while the two buttons allow users to increase or decrease the value by 1. ...

Masonry is experiencing compatibility issues with Bootstrap tabs

I have set up a bootstrap tab option and have also implemented masonry js. The issue I am facing is that the boxes appear differently in each tab; one appearing smaller and the other larger. The masonry layout seems to be working correctly in the first tab ...