Utilizing AngularJS with dual controllers within a single view

I recently started working on an angularJS web app using a purchased template that has all its controllers, routes, and directives in a single file called app.js.

This is my first dive into angularJS and front-end development. Hoping to become a Full Stack geek now.. :D

However, I've hit a roadblock. The app functions perfectly fine until I try to add a new controller AuthCtrl from another file named auth.js. It throws an error saying AppCtrl not found. Below HTML snippet highlights the exact issue:

<!doctype html>
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->

<!-- rest of the provided HTML content stays same -->

<span class="highlight">/*Code ommitted for brevity*/</span>

    <script src="scripts/controllers/auth.js"></script>

<span class="beginning">(function() {</span>
<span class="content-indent">"use strict";</span>
angular.module("app.chart.ctrls",[])
    .controller("chartCtrl",[
        "$scope",
        function($scope){ ....

Answer №1

The issue here lies in the implementation within the auth.js file:

var app = angular.module('app',.......

This code snippet is creating a new instance of the 'app' module or potentially overwriting an existing one declared in the app.js file.

Instead of this, you should aim to utilize the same 'app' variable that has been defined in the app.js file.

To achieve this, it is recommended to declare the app and store a reference to it in a separate variable:

var yourApp = angular.module("app.controllers",[.........]);

You can then link controllers by assigning them to the variable containing the app definition. As an example, let's explore how the 'chartCtrl' controller should be associated with the 'yourApp' application:

yourApp.controller("chartCtrl",[
        "$scope",
        function($scope){ ....

In your auth.js file, update the controller implementation as follows:

yourApp.controller('AuthCtrl', function ($scope) {
  $scope.phones = [
    {'name': 'Nexus S',
     'snippet': 'Fast just got faster with Nexus S.'},
    {'name': 'Motorola XOOM™ with Wi-Fi',
     'snippet': 'The Next, Next Generation tablet.'},
    {'name': 'MOTOROLA XOOM™',
     'snippet': 'The Next, Next Generation tablet.'}
  ];
});

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 include multiple ng-repeats within a single ng-repeat?

Hey, I'm facing quite a tricky situation with this grid and could use some assistance. I'm trying to figure out how to populate the data using ng-repeat. I've attached an image showcasing the desired layout of the grid and the order in which ...

What steps should I take to troubleshoot and resolve the connection issue that arises while trying to execute npm install

Following the guidelines from: https://www.electronjs.org/docs/tutorial/first-app I executed commands like mkdir, cd, and npm init. They all ran successfully, generating a file named package.json. Subsequently, I entered npm install --save-dev electron w ...

What could be causing my JavaScript pricing calculator to malfunction?

I've been struggling to make the script below work, but I can't seem to figure out why. It should be functioning properly. Even though all variables are in place, no price is appearing? You can view the HTML on this page: var cake_prices = n ...

What is the process for deploying a next.js application with a custom express backend to a VPS or Heroku platform?

Does anyone have advice on deploying a next.js application with a custom express backend to either a VPS or Heroku? ...

What is the best method for iterating through an array and generating a glossary list organized by categories?

I have an array filled with definitions. How can I use Vue.js to iterate through this array and create a glossary list organized by letters? Desired Output: A Aterm: A definition of aterm B Bterm: A definition of bterm C Cterm: A definition of cterm ...

Encountering a Vue syntax error following the binding of a session variable

Encountering a syntax error while attempting to bind a session variable as a prop of my Vue component. Scrutinizing my code did not reveal any mistakes, but perhaps another set of eyes may catch something. This is where I have registered my components: V ...

Utilizing Angular's Dependency Injection in Component Development

Can component injection be achieved in AngularJS using dependency injection? There is a SignalR hub factory that I want to use for injecting my SignalR hub proxy. Below is the code snippet where I am creating an ngTable Component that needs to be updated ...

Encountered a problem with AngularUniversal prerendering: UnhandledPromiseRejectionWarning: Unable to locate NgModule metadata for 'class{}'

Objective The task may seem lengthy, but it's straightforward! Currently, I am utilizing Angular Universal for Server-Side Rendering (SSR) by following a tutorial. The Universal/express-engine has been installed, main.js is generated in the dist/pro ...

Display a Button in AngularJS When the Model is Modified

Within an ng-repeat, I have a model: <tr ng-repeat="expert in experts"> <td>{{expert.expertName}}</td> <td ng-mouseleave="editMode = false"> <span ng-hide="editMode" ng-click=" ...

Is there a way to deactivate the onClick event when the dropdown placeholder is chosen?

I have experimented with different methods to prevent the onClick event when selecting either placeholder, but I have not been successful. Here is my current code: <div class="choosesign"> <div class="zodiacs"> < ...

Tips for preserving both existing data and new data within React's useState hook in React Native or ReactJS?

As I dive into learning reactjs, one question that has been on my mind is how to store both previous and upcoming data in useState. Is there a special trick for achieving this? For example: Imagine I enter "A" and then follow it with "B". My goal is to ha ...

Activating gzip compression using fetch.js

I am utilizing fetch.js (https://github.com/github/fetch) to transmit a rather substantial JSON object to the backend. The size of the JSON is significant due to it containing an SVG image string. My question is whether fetch.js applies gzip compression a ...

Discover the power of Vue.js 3 by seamlessly integrating anonymous functions within event handlers

Is it possible to invoke an anonymous function within an event handler in Vue.js 3? I came across several options on various websites: <button @click="return function() { console.log('test')}()">Click me</button> <butto ...

Unit Testing Angular: Mastering Unit Testing for the .map() Function

I am in need of testing a service method, but I am unsure about how to achieve complete coverage for the code that gets executed as a result of calling another injected service method. The Service Method to be tested: @Injectable() export class BomRevisi ...

Incorporate jQuery on elements that are dynamically loaded after the page has finished loading

I need to determine if a dynamically added button is enabled or disabled. How can this be achieved? My goal is to display a message when the button is disabled and remove it when the button becomes enabled. This is my current code: jQuery(document).read ...

Mastering Puppeteer: Tips for Successfully Submitting Forms

Can you use puppeteer to programmatically submit a form without a submit input? I have been successful with forms that include a submit input by using page.click('.input[type="submit"]'), but when the form does not have a submit input, focusing o ...

React setState issue experienced only on initial click due to API lag

My goal is to develop a web app using the Poke API to display multiple Pokemon. I have added buttons to allow users to "change the page" and switch the API URL to view the next or previous set of Pokemon. However, I'm facing an issue where the buttons ...

Editable Table Component in React

I've developed a React table as shown below: const CustomTable = ({content}) => { return ( <table className="table table-bordered"> <thead> <tr> <th>Quantity</ ...

Having trouble with adding a class on scroll?

My challenge is to extract the header from this website, by adding an additional class when the user scrolls at a position greater than 0. I thought it would be easy, but Java always presents problems for me. Here’s the code I came up with: <!DOCTY ...

What strategies can I use to ensure that I can successfully send 3,000 requests to the Google Drive API using node.js without surpassing

I'm currently assisting a friend with a unique project he has in mind. He is looking to create 3000 folders on Google Drive, each paired with a QR code linking to its URL. The plan is to populate each folder with photos taken by event attendees, who ...