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

Displaying a dynamic menu using Angular's ngFor loop

I am attempting to create a menu with sub-menus. The desired structure for my menu is outlined below. However, the demo I'm testing is not producing the correct structure. Check out the demo here. "Sub Test": { // Main menu "Example1":"hai",//sub ...

Obtaining the file size on the client side using JSF RichFaces file upload

I am currently using version 4.3.2 of rich fileupload in JSF richfaces. The issue I am facing is that the files first get uploaded to the server and then an error is thrown if the file size exceeds a certain limit. Even though I have set a size restriction ...

Angular directives with templateUrl pointing to 'from my own server'

Hello there, I am a newcomer to Angular and I have a small inquiry. Currently, I am successfully rendering my partials using the $routeProvider from the server, but I am uncertain if the same can be achieved from a directive? For example: myapp.directiv ...

Utilizing JQuery AJAX to Submit a JSONP Form with File Attachments

I have been working on implementing a contact form for our clients who create websites with their own domains through us. As part of our services, we provide hosting and a web editor to help them build their websites. The contact form they include on their ...

Can we combine JQuery includes with Angular Includes?

I have come across an issue while working on an application that combines both Jquery and AngularJS includes. It seems that Angular does not work properly after Jquery has included a file containing AngularJS markup. Specifically, Jquery is including the " ...

Methods for initializing state before each test in AngularJS

Within my test setup, a service is injected using beforeEach: beforeEach(inject(function($rootScope, $state, $injector, $controller, MyService) { var state = $state; scope = $rootScope.$new(); myService = MyService; ctrl = $controller(&apo ...

The issue with AngularJS Routing is that it fails to refresh the menu items when the URL and views are being

My current project involves implementing token-based authentication using the MEAN stack. The goal of my application is to display different menu items based on whether a user is logged in or not. When there is no token present, the menu should show option ...

Undo a CSS transition when clicked

There is a div named learn-more which expands to fill the whole page when a CSS class is added like this: .learn-more{ padding:10px; font-size: 20px; text-align: center; margin-top:20px; font-family: klight; -webkit-transition:2s; ...

Problem with full-page navigation sliding in and fading in and out

Upon the user's click on <a href="#slide-nav" class="slide-nav-trigger">, a full-page navigation smoothly slides into view. This animation is triggered by CSS and uses jQuery for event delegation. The Dilemma Instead of abruptly turning on and ...

Attempting to transfer a property from one page to another using the Link component in NextJS

Currently, I have a page containing six Link elements that are meant to redirect to the same destination but with different props based on which link is clicked. To pass props, this is how I've implemented it: <Link href={{ pathname: '/pro ...

Passing PHP information into a JavaScript array

I am facing an issue with my PHP file where I am fetching data from a MySQL database and storing it in a PHP array. I am then trying to output this data as a JS array but for some reason, I am unable to access the JS variable in my JS files. Here is the c ...

Ways of retrieving Sveltekit session data in an endpoint

Is there a way to access a session in an endpoint using SvelteKit? I attempted the following with no success: import { get } from 'svelte/store'; import { getStores} from "$app/stores"; function getUser() { // <- execute this du ...

Error: Element type is invalid: a string was anticipated, but not found

I recently experimented with the example provided in React's documentation at this link and it functioned perfectly. My objective now is to create a button using material-ui. Can you identify what is causing the issue in this code? import * as R ...

tips for adjusting font size of toolbar in ionic framework

I have successfully created a toolbar, but now I want to customize it further. Specifically, I am looking to set different default font sizes for the buttons and adjust the default width of each button. Currently, my button bar looks like this: <div c ...

Issue with passing boolean parameter in Javascript not functioning properly

I have a function that contains multiple if statements, designed to execute when a parameter is true. However, I've noticed that passing false as a parameter does not seem to have any effect. Can you help me figure out what I'm doing wrong? fu ...

the ultimate guide to leveraging a single slot to edit various columns within data tables

Utilizing vuetify, I have successfully created a reusable data table. The headers and items are passed as props to allow for the data table to be used in various components. While employing slots, I have taken a unique approach by implementing a column-ba ...

Determining the total number of combinations possible from a collection of five arrays containing items

I have five massive collections filled with various strings, and each of them contains a different number of elements. Below are examples of the lists: List 1 "Jeffrey the Great", "Bean-man", "Joe", "Charles", "Flamur", "Leka", ...

Vue.js: Optimize Webpack bundle by excluding core-js

Below is the content of my vue.config.js file: module.exports = { configureWebpack: { externals: { "vue": "Vue", "core-js": "core-js", }, }, }; By using this configuration, I have successfully excluded the vue.js (Vue) library and ...

Fixing 404 Errors in Angular 2 Due to Component Relative Paths in SystemJS-Builder

I recently posted this on https://github.com/systemjs/builder/issues/611 My goal is to bundle my Angular 2 rc 1 application using systemjs-builder 0.15.16's buildStatic method. In my Angular component, there is a view and one or more external stylesh ...

Experiencing difficulties with JWT implementation and seeking to transmit the JWT token to additional APIs

I am currently working on implementing JWT authentication in node js/express js: Below is the sample code I have written for this purpose: const jwt = require('jsonwebtoken'); const secretKey = crypto.randomBytes(64).toString('hex'); c ...