AngularJS Controlling the Flow

Recently delving into AngularJS, I managed to set up a basic routing system with two controllers. However, the second controller seems to be inactive and troubleshooting has proven difficult.

app.js:

angular.module('foodListerApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch']).config(function ($routeProvider) {
$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl',
    controllerAs: 'main'
  })
  .when('/about', {
    templateUrl: 'views/about.html',
    controller: 'AboutCtrl',
    controllerAs: 'about'
  })
  .otherwise({
    redirectTo: '/'
  });
});

MainCtrl (main.js):

angular.module('foodListerApp').controller('MainCtrl', ['$scope' , $window , $location ,  function ($scope , $window , $location) {



    console.log("In Main Controller ... ");


    $scope.onSubmit = function() {
        $window.location.href = 'views/about.html';
    }; 
}]);

AboutCtrl (about.js):

angular.module('foodListerApp').controller('AboutCtrl', ['$scope' , function ($scope) {
    console.log("In 'about' controller ..."); 
}]); 

The HTML files are omitted, but they seem fine. The "onSubmit" method is functional in the first controller, however, the second controller remains silent without any output, leaving me puzzled about the root cause.

Answer №1

angular.module('foodListerApp').controller('MainCtrl', ['$scope' , $window , $location ,  function ($scope , $window , $location) {

Remember to include quotes in

angular.module('foodListerApp').controller('MainCtrl', ['$scope' , '$window' , '$location' ,  function ($scope , $window , $location) {

You missed adding quotes around them.

Answer №2

Utilize the location provider for navigating between routes.

$location.path( 'views/services.html');

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

Incorporating a new data series into your candlestick chart with Highstock

I've encountered an issue with adding a data series to my candlestick chart in Highstock using Highcharts. Here's the script I'm using: $.ajax({ url : 'indicator', type : 'GET', data ...

Unleashing the Power of Node.js: A Step-by-Step Guide to Crafting

I am developing a console interface that prompts the user with questions and receives input through the console. Some questions require the user to provide a limited number of inputs. I have researched ways to obtain console input in node js, but I haven&a ...

Encountered errors while trying to install ng-bootstrap, installation of package was unsuccessful

I am currently developing an Angular application and I require an accordion component for it. My efforts to install the ng-bootstrap package using the command 'ng add @ng-bootstrap/ng-bootstrap' have been unsuccessful as the installation fails ev ...

The values obtained from the previous parameter object of the React setState hook can vary and are not always

In my code, I am using a useEffect hook to update the state with setState. However, I'm encountering some unusual and inconsistent behavior with the previous parameter: useEffect(() => { setCurrentPicturesObject((existing) => { ...

Metadata pertaining to the duration of blob videos

I have developed a software that processes camera stream video specifically in Firefox. Using the MediaRecorder API, I create a Blob containing video recordings. To save this blob as a video in local storage, I utilize the FileSaver library: FileSav ...

Chat Bot - EmbedMessage

JavaScript is still very new to me and I find the actual code quite challenging to grasp. However, I do have one specific question - how can I display the "cahbResponses" in a MessageEmbed? Despite consulting the Discord JS guides on MessageEmbeds, I' ...

Guide to setting a generic key restriction on a function parameter

Today, I decided to have some coding fun and try creating a generic pushUnique function. This function is designed to check if a new object being added to an array is unique based on a key, and then push it if it meets the criteria. At this point, all I h ...

Is integrating Vuetify with a project template created using vue-cli 3 causing issues when adding script tags to App.vue?

As a Vue beginner, I'm trying to wrap my head around a basic concept. In vue-cli 3, the "App.vue" file serves as the root component with <script> and <style> tags like any other component. However, after adding Vuetify to a project, the Ap ...

Creating a new form upon a button click event dynamically in PHP

I have a situation where I've created dynamic buttons in PHP. When clicking on one of these dynamic buttons, I need to open a new form on the same homepage and prevent the page from refreshing. How can this issue be resolved? Below is the code that I ...

Can you explain the distinction between these two concepts in JavaScript?

I've come across an issue while assigning PHP values to JavaScript. var a = <?php echo 39; ?> JavaScript is throwing an error that says "Uncaught SyntaxError: Unexpected token ILLEGAL". However, when I assign PHP values in a slightly different ...

When making a request with XMLHttpRequest(), FormData() is not being sent, causing the PHP $_FILES array to be empty and the

My Objective: I am attempting to achieve the task of uploading a file from a user to a specific directory on the server using an HTML <input type="file"> element. To accomplish this, I have implemented a new XMLHttpRequest within the <input> ...

Tips for building dynamic drop downs with JavaScript?

I'm currently working on a personal project and I've hit a roadblock when it comes to saving and utilizing the selections made from dropdown lists. For instance: <select id = "CarType" onchange = "cartype()"> <option value = "car"&g ...

An undefined variable was encountered within the 'else' statement

I am encountering an issue with my code where it is returning an 'undefined' error. The problem arises when I attempt to remove an id from an array using the variable 'id', but instead, it throws an error stating 'undefined'. ...

Navigating through files with JavaScript

I've encountered an issue while trying to write an array to a file. The code snippet is as follows: for(i=1;i<tblRows;i++) { facID=tbl.rows[i].cells[1].innerHTML; money=tbl.rows[i].cells[2].innerHTML content+=facID+','+money+ ...

Angular Typeahead must retrieve object properties

For my project, I am utilizing the Angular Typeahead feature to display data retrieved from an $http call. The information is stored in an array of objects like [{"abbrev":"FL", "state":"Florida"}, {"abbrev":"VA", {"state":"Virginia"}]. Here is a snippet o ...

Guide on verifying the status of a switch toggle using Selenium and C#

I am having trouble verifying the current state of a switch ('on' or 'off') using selenium. The toggle appears in the 'on' position when the webpage loads, but it affects filter results in a table when switched off. I am unabl ...

Hide panel when button is clicked - bootstrap

Is it possible to make a panel collapse by clicking a regular bootstrap button? Below is the code snippet: <div class="panel panel-primary" style="border-color: #464646;"> <div class="panel-heading" style="border-color: #BBBBBB; height: 35px; ...

Encountering the error message "value is not defined" when using a React range slider

I recently tried setting up the nodejs react range slider component by following the instructions provided on https://www.npmjs.com/package/react-rangeslider. Despite installing all the necessary dependencies, I keep encountering an error message stating ...

Handling Circular Dependency: Injecting a Service into an HTTP Interceptor in AngularJS

I'm currently working on developing an HTTP interceptor for my AngularJS application to manage authentication. Although the code I have is functional, I am wary of manually injecting a service as I believed Angular is designed to handle this process ...

Using Angular ng-token-auth to post to an incorrect localhost address

I have a Node.js server running on http://localhost:3000/, and a React Frontend running on http://localhost:8000/. I am setting up the user authentication feature with JWT. However, when posting the token, it goes to the incorrect localhost address: http ...