AngularJS routing not rendering the correct view

I'm encountering a routing problem with my AngularJS/ExpressJS application. The issue is that login.ejs and signup.ejs are partial views, while welcome.ejs serves as the main template. The intention is for these views to load in a ui-view container within the welcome.ejs page. When accessing /welcome, the welcome.ejs page should appear with the default login form. However, both /welcome/login and /welcome/signup URLs only display the /welcome page with the login form.

authController.js:

var app = angular.module("myapp.controllers.auth", [
  "ui.router"
]);

app.config([
  "$stateProvider",
  "$locationProvider",
  function($stateProvider, $locationProvider) {
    $stateProvider.state("welcome", {
      url: "/welcome",
      templateUrl: "/welcome",
      controller: "AuthController",
    })
    .state("login", {
      parent: "welcome",
      url: "/login",
      views: {
        "container@": {
          templateUrl: "auth/login",
          controller: "AuthController"
        }
      }
    })
    .state("signup", {
        parent: "welcome",
        url: "/signup",
        views: {
          "container@": {
            templateUrl: "auth/signup",
            controller: "AuthController"
          }
        }
      });
    $locationProvider.html5Mode(true);
  }
]);

Relevant ExpressJS code:

var express = require("express");
var router = express.Router();

router.route(/\/welcome.*/)
  .get(function(req, res, next) {
    res.render("welcome");
  });

There seems to be a minor oversight causing this issue, any suggestions?

EDIT: Including HTML code

welcome.ejs:

<html>
<head>
  <base href="/">
</head>
<body ng-app="myapp">
  <div ui-view="container" class="auth-container">
    <span>Sign in</span>
    <form ng-submit="login()">
      <div class="form-group">
        <input type="text" ng-model="user.email" autofocus>
      </div>
      <div class="form-group">
        <input type="password" ng-model="user.password">
      </div>
      <button type="submit" class="btn btn-primary">Log In</button>
    </form>
    <a ui-sref="signup">Sign Up</a>
  </div>
  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
  <script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
  <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
  <script type="text/javascript" src="/javascripts/app.js"></script>
  <script type="text/javascript" src="/javascripts/controllers/authController.js"></script>
  <script type="text/javascript" src="/javascripts/services/authService.js"></script>
</body>
</html>

auth/login.ejs

<span>Sign in</span>
<form ng-submit="login()">
  <div class="form-group">
    <input type="text" ng-model="user.email" autofocus>
  </div>
  <div class="form-group">
    <input type="password" ng-model="user.password">
  </div>
  <button type="submit" class="btn btn-primary">Log In</button>
</form>
<a ui-sref="signup">Sign Up</a>

auth/signup.ejs:

<span>Sign Up</span>
<form ng-submit="signup()">
  <div class="form-group">
    <input type="text" ng-model="name" autofocus>
  </div>
  <div class="form-group">
    <input type="text" ng-model="user.email" autofocus>
  </div>
  <div class="form-group">
    <input type="password" ng-model="user.password">
  </div>
  <button type="submit" class="btn ban-primary">Sign Up</button>
</form>
<a ui-sref="login">Login</a>

Answer №1

When you use /welcome to access your welcome.ejs file, representing the root state (usually index.html), there is no need for a specific welcome state. The issue arises from re-writing the URL to welcome.ejs using the /\/welcome.*/ regex route, which means any state URLs under it must have the /welcome prefix. To address this, create an abstract parent state to establish the /welcome URL prefix.

$stateProvider.state('welcome', {
    abstract: true,
    url: '/welcome'
}
.state("login", {
  parent: 'welcome',
  url: "/login",
  views: {
    "container@": {
      templateUrl: "auth/login",
      controller: "AuthController"
    }
  }
})
.state("signup", {
  parent: 'welcome',
  url: "/signup",
  views: {
    "container@": {
      templateUrl: "auth/signup",
      controller: "AuthController"
    }
  }
});

Alternatively, remove the welcome state and add /welcome as a prefix to all state URLs, like so:

url: '/welcome/login'

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

Display JSON data in AngularJS and show the corresponding data on the right side when clicked

I'm brand new to using AngularJS and I've been struggling with this issue. Take a look at the code snippet below to see what I mean. The titles are shown on the left, and when you click on one, I want the corresponding body to display on the righ ...

What steps are necessary to configure .eslintrc to identify the use of 'require'?

I recently started using ESLint and successfully integrated it with IntelliJ. Initially, ESLint did not recognize node out of the box. After consulting the documentation, I created a configuration file named .eslintrc at the project's root folder, sp ...

Sharing information between controllers while being initiated using $rootScope.$emit

I've created a common module that includes a controller, component, and template to initialize the app and set up the base layout. Within this module, there is also a stateful component that makes an HTTP GET request during initialization to fetch a b ...

AngularJS controller's scope variable retains its values upon revisiting the view, without getting cleared

Currently, I am developing an AngularJS client application that interacts with SignalR 2.2. Furthermore, the Angular application utilizes the Angular UI Router library. This is what my hub class, MessagingHub.cs, looks like: public class MessagingHub : H ...

Save a customized JavaScript file and integrate it into a Vue component

As a newcomer to Vue.js, I have a question regarding calling functions from a custom JavaScript file within a Vue component. Here is what I attempted: custom.js class API{ function testCall(){ alert("test ok"); } } export {API} App.vue ...

What purpose does the class serve in typescript?

This is a unique version of app.component.ts in the Angular Tour of Hero tutorial. import { Component } from '@angular/core'; export class Superhero{ name : string; id : number; } const SUPERHEROES : Superhero[] = [ {name : 'Wonder ...

Use `$$state: {…}` within the request rather than the data

When attempting to send a request with data, I am only getting "f {$$state: {…}}" in the console. $scope.createTask = function () { var req = $http.post('api/insert', { title: $scope.newTitle, description: ...

Is there a way to display the back and forward buttons on a popup window opened through window.open or self.open in Chrome browser?

When I try to open a popup window using the code snippet below, I am facing some issues. self.open('myJSPPage','ServicePopUp','height=600,width=800,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes'); Afte ...

Retrieving JQuery Results Based on List Items

Once I have obtained the list id from the navigation, my next step is to send the id to a PHP file that will return a JSON list. Below is the jQuery syntax I am using: $("#ul_navigation li").click(function(e) { idsec = this.id; alert(idse ...

Integrating fresh components into a JSON structure

I've been attempting to insert a new element into my JSON, but I'm struggling to do it correctly. I've tried numerous approaches and am unsure of what might be causing the issue. INITIAL JSON INPUT { "UnitID":"1148", "UNIT":"202B", "Sp ...

Tips for updating and transferring a variable within a callback function

I have implemented a function using the SoundCloud API that retrieves a song URL, obtains the associated sound ID from the API, and then passes that ID to a callback for streaming purposes and updating the page. The data is successfully retrieved from the ...

Displaying two distinct tables utilizing ng-repeat by employing customized directives

I'm facing an issue with a custom directive that generates tables and is appearing twice on my index page. The data for these tables comes from the $scope.globalrows variable. Even though globalrows contains 2 arrays, it always displays the second arr ...

The asynchronous ajax function fails to work properly when setInterval is activated

My issue is that only the initial execution of the updateProgress function happens while waiting for the completion of syncDNS. All subsequent calls made via setInterval remain on hold until syncDNS finishes. Can anyone explain why this is happening? $( ...

Tips for avoiding problems with quoting and using apostrophes in a JavaScript function inside a tag in a JSP file

Within my JSP, I have a string value stored in ${state.status.code} that I need to pass to a JavaScript function when a table element is clicked using onClick to trigger the showStatus function. Here is how I have attempted to achieve this: <c:set var= ...

Error: The property 'combine' of 'winston_1.default.format' cannot be destructured since it is not defined

Encountered an error while using Winston in Node.js, how can we resolve it? The version of Winston I am using is 3.3.3 and winston-daily-rotate-file version is 4.5.0 I attempted npm i winston@next --save, but the error persists. ** Here is the Error Mes ...

Incorporate a dynamic PowerPoint presentation directly onto my website

In my situation, on the client side, users can select image files (jpg|png|gif), PDF files, and PPT files which are then stored in a database. When viewing these selected files in the admin panel, I am using conditional statements to display them appropr ...

"Node.js is giving an error stating that the object does not have a

I am attempting to save the user details from the registration form into a JSON file for authentication purposes. However, I am having trouble appending the data in the correct format. Here is the code snippet that I have tried: var filename = "./user_log ...

Issue with undefined object in ExpressJS PUT method

Attempting to utilize the PUT method for updating a record in my database, I encountered an issue with the object not being defined. ReferenceError: blogpost is not defined Following this tutorial for routing steps, I noticed that while the variable is d ...

Switching images upon hovering in AngularJS

Looking to change images on hover in my Angular app, encountered site peculiarities making CSS solution impractical. Resorted to using ng-mouseenter and ng-mouseleave for image swapping instead. landing.jade img.share-button(src='images/btn_share.p ...

What is the best way to send the index of an array to a onClick event in ReactJS?

const DisplayInventory = ({ items }) => <div className="row"> {items.map((item, i) => <div className="item" key={"item_" + i}> <div className="card hoverable col s3"> <img onClick={purchase ...