Navigating through different sections of your Angular app can be easily accomplished using

My current project structure is set up like this:

The application I am working on is hosted in a subdirectory called /my-scripts/vk.plants. This means that when I request this URL, it loads layout.html and returns it to the user.

layout.html contains the following code:

<!DOCTYPE html>
<html lang="en" ng-app="app">
    <head>   
        <script src="/my-scripts/vk.plants/public/static/scripts/angular.js"></script>
        <script src="/my-scripts/vk.plants/public/static/scripts/angular-route.js"></script>

        <script src="/my-scripts/vk.plants/public/static/scripts/angular/app/app.js"></script>
        <!-- include controllers and services -->
    </head>

    <body>
        <h1>Angular Layout!!</h1>

        <div ng-view></div>
    </body>
</html>

And app.js looks like this:

(function(angular) {
    'use strict';

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

    app.config(function($routeProvider) {
        $routeProvider
            .when('/my-scripts/vk.plants', {
                controller: 'home.controller',
                templateUrl: '/my-scripts/vk.plants/public/static/scripts/angular/app/templates/home.html'
            });
    });
})(angular);

When attempting to open

http://my-url/my-scripts/vk.plants
, I receive the message from layout.html, but the requests for my templates/home.html are not being processed and nothing from this template is displayed:

I am facing confusion with Angular routing as there are no errors showing in the console. Can someone provide an explanation?

What I require: When opening

http://my-url/my-scripts/vk.plants
, I would like to set the default controller and view for this URL in Angular as home.controller and home.html.

Answer №1

Alright, here's a guide on how to set up your paths.

I recommend changing your controller name from 'home.controller' to 'homeCtrl' for simplicity. However, you can still use 'home.controller' if you prefer, but the suggested way is better.

Let's start with home.controller...

<!DOCTYPE html>
<html lang="en" ng-app="app">
    <head>   
        <script src="/my-scripts/vk.plants/public/static/scripts/angular.js"></script>
        <script src="/my-scripts/vk.plants/public/static/scripts/angular-route.js"></script>

        <script src="/my-scripts/vk.plants/public/static/scripts/angular/app/app.js"></script>

           //INCLUDE starCtrl.js (you have to create this)
           //include home.controller.js 

        <!-- include controllers and services -->
    </head>

    <body   ng-controller="startCtrl">     // initialize startCtrl here
        <h1>Angular Layout!!</h1>

        <div ng-view></div>
    </body>
</html>

startCtrl.js

app.controller('startCtrl',function(){
     $location.url('home')     //startCtrl gets initialized and it redirects you to home (it will check for defined routes
}) ;

Now, to your app.js

(function(angular) {
    'use strict';

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

    app.config(function($routeProvider) {
        $routeProvider
            .when('/home', {                 //home route found
                controller: 'home.controller',   //controller will be applied
                templateUrl: '/my-scripts/vk.plants/public/static/scripts/angular/app/templates/home.html'  // user will be redirected to this page.
            });
    });
})(angular);

I hope this explanation helps. With startCtrl, you can explore more features that might be new to you. Follow these steps and let me know if you encounter any issues or errors in the console.

Answer №2

I have come up with a solution to my issue in the following manner:

  • Firstly, I added a base element to the layout.html:

    <base href="/my-scripts/vk.plants/" />
    
    <script src="public/static/scripts/angular.js"></script>
    <script src="public/static/scripts/angular-route.js"></script>
    
    <script src="public/static/scripts/angular/app/app.js"></script>
    
  • Next, I modified the module configuration as follows:

    (function(angular) { 'use strict'; 
    var app = angular.module('app', ['ngRoute']);
    
    app.config(function($routeProvider) {
        $routeProvider
            .when('/', {
                controller: 'home.controller',
                templateUrl: 'public/static/scripts/angular/app/templates/home.html'
            })
            .when('/factor', {
                controller: 'factor.controller',
                templateUrl: 'public/static/scripts/angular/app/templates/factor.html'
            });
    });
    

    })(angular);

The outcome of these changes is reflected below:

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

Choose key and value pairs in AngularJS

Currently, I am retrieving JSON values from an HTTP service for each ID and attempting to create a dropdown with a list of names and IDs as keys to store. Below is the HTML code snippet: <select> <option ng-repeat="(key, value) in data" value="{{ ...

Inverting Arrays Recursively with JavaScript

Currently, we are delving into functional JavaScript in my programming course with a particular assignment. However, I seem to be struggling to make the code work as intended. I would greatly appreciate any advice on how to improve this piece of code. Ever ...

Using jQuery Datatables: Storing the received JSON data into a variable

I have incorporated 2 plugins for my project: Datatables (used for pagination) Defiant.js (providing JSON search capability) Describing my issue along with the code snippet... $(document).ready(function() { var myjson; //Initializing Datatable ...

Exploring Angular 9: Harnessing the Power of Fork Join with an Array of

I have a challenge where I need to send multiple API requests to an endpoint by iterating over an array of values To handle this, I decided to use rxjs library and specifically the forkJoin method //array to keep observables propOb: Observable<any>[ ...

How can we deliver pure JS, HTML, and CSS content without relying on static HTML pages?

Looking to create a fast app prototype without using React or Vue? I'd like to avoid simply making an html and js file imported within it. Can npm packages, SCSS be used while programming vanilla Javascript minus a framework? ...

Split the JSON response into different arrays

I am attempting to split this JSON response into multiple arrays that I can utilize on the front end. The JSON response looks like this: { "clients": [ { "id": "2", "f_name": "test", "l_name": "test", "email": "<a href="/cdn-cgi/l/email-protec ...

Serving sourcemaps for a web extension in Firefox: A step-by-step guide

Currently in the process of developing a web extension using TypeScript, I have encountered an issue with sourcemaps not loading properly. The use of parcel to bundle my extension has made the bundling process simple and straightforward. However, while the ...

$uibModal displaying wrong information when clicked for the first time

I'm having an issue with $uibModal in ServiceNow. When I try to open a modal window containing an embedded widget, it opens correctly on the first click but requires two clicks for the content to load properly on subsequent clicks. Some sources sugges ...

Fetch information from the input field and send it to the Redux state

Looking for a solution - I'm working on a simple todo app where I need to store user input value in the redux state. The input value is currently stored in local state until the user clicks the "add" button. The main issue : How can I pass this input ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

ReactJS how to prevent accordion from collapsing automatically

My custom accordion layout for the features needed in the site I am building is not working well with Jquery. How can I modify it to collapse properly? Specifically, I want it so that when I open number 2, number 1 will automatically close. I've trie ...

Step-by-step guide on incorporating a new JSON object into an array to display its elements as components on a webpage

Could I adjust the state of an array by incorporating values from a data.js file as a starting point? The process involves executing the setAllThingsArray function to add a new element, determined by the setThingsArray function based on the previous state ...

Encountering difficulties during the migration process from a JavaScript to a TypeScript React Component

I've encountered some challenges with configuring TypeScript in my project. Initially, I developed my application using plain JavaScript. However, eager to learn TypeScript, I decided to convert my JavaScript project into a TypeScript one. To achiev ...

Discover how to retrieve full response data by entering text into a text field using Reactjs

I am encountering an issue while retrieving values from rendered data in a component that has already been displayed on the page. I want to input data into a text field and have it sent to the database, but using the runtime data from that specific field. ...

Tips for altering the appearance of a button when moving to a related page

I have a master page with four buttons that have a mouse hover CSS property. Each button's corresponding response page is defined on the same master page. Now, I want to change the button style when the user is on the corresponding page. How can this ...

Utilizing Node.js and Express alongside EJS, iterating through objects and displaying them in a table

Today I embarked on my journey to learn Node.js and I am currently attempting to iterate through an object and display it in a table format. Within my router file: var obj = JSON.parse(`[{ "Name": "ArrowTower", "Class" ...

Utilizing a filter within the ng-model directive

I have a question about using a filter with an h3 element. Here is the code snippet: {{ event.date | date:'dd-MM-yyyy' }} It's working perfectly fine and Angular is formatting the date as expected. However, when I try to use the same filte ...

Utilizing Google App Engine for seamless deployment, integrating Ajax for dynamic interactions, and

Using the google App Engine, I am looking to implement javascript (or Ajax) for POSTing a form and updating the target div. The form includes multiple fields and files for transfer. The javascript function I am using is extracted from the "Javascript: The ...

Chip input component in React

I've recently upgraded to React 17 and encountered an issue with chip input fields like material-ui-chip-input not working properly. I've tried various npm packages, but none of them seem to be compatible with React version 17. Does anyone know ...

What is the best way to send an HTML form variable to a Perl script using AJAX?

My goal is to pass the HTML variable from the list menu to the Perl script using POST. However, when I try to do this, the jQuery ajax() function executes the Perl script without including the value obtained from document.getElementById. Below is the sni ...