Is it possible to utilize a controller within a page by referencing its controller ID?

In my project, I am facing an issue where multiple HTML pages are linking to each other and duplicating the same controller each time a new page is opened. This results in each page using its own controller, causing data saved in the previous controller to not apply on the next page with the new duplicated controller.

Upon realizing that each controller has its own ID ($scope.$id), I have a couple of questions:

  • Is there a way for me to specify which controller should be used on an HTML page based on its controller ID?

  • Alternatively, is there a different method to store controller variables in a separate location so they do not reset on each page?

Answer №1

<div ng-controller="myCtrl">
            <div ng-switch on="condition">
                <div ng-switch-when="condition_output_1">
                   <div ng-include="'html_page_2_url.html'"></div>
                </div>
                <div ng-switch-when="condition_output_2">
                     <div ng-include="'html_page_1_url_2.html'"></div>
                </div>
            </div>
    </div>

Switching the html_page_1_url.html and html_page_2_url.html will not affect the functionality of the code.

myCtrl will remain constant across all instances.


To share data between controllers, you have multiple options:

  • $rootScope
  • services
  • localstorage

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

Unable to assign a local variable in Jquery .ajax() function to a global variable

Check out this example of a jQuery AJAX code: $(document).ready(function() { var custom_array = new Array(); $.ajax({ url: 'data.json', type: 'get', dataType: 'json', success: function(response) { $ ...

What is preventing me from installing React and uninstalling create-react-app from my system?

I've exhausted all my options but still can't seem to figure out how to install ReactJS on my system. Every time I complete the installation process, I encounter this error message: A template was not provided. This is likely because you' ...

Error message: "SyntaxError: Unexpected token import was not caught by the foundation"

I have recently taken over development from a previous developer who integrated Zurb Foundation as a framework into our website. The Foundation framework was installed using npm. I am encountering errors in the console related to all of the foundation java ...

Transforming text elements into JSON format

My text contains a list of items formatted as follows: var text = "<li>M3-2200 (da2/M3-2200)</li><li>N3-2200 (da2/N3-2200)</li><li>Picasso (picasso/A500)</li><li>Picasso (picasso/A501)</li><li>Picasso ...

Issues encountered with Three.js MeshBasicMaterial functionality

I am currently working on generating a texture using Three.js. The texture_f1 source I am using is a .png file, which allows the background to show through. The issue arises when attempting to set the background color using color: 0xffffff in conjunction ...

Implementing ng-show based on a specific position on a webpage

I am a beginner in AngularJS and I'm struggling to understand how to customize ng-show/ng-hide. I want the navigation bar below to only appear once the user has scrolled 600px down the page, but since I can't use jQuery with Angular, I'm uns ...

"Email verification is always set to true for users signing in through Firebase

Currently, I am working on integrating a sign-in form using Firebase web and Vue.js. The issue I'm facing is that I need to send a verification email to confirm the user's email address, but the emailVerified key is always set to true by default ...

Acquire a JSON response from a web address by utilizing JavaScript

If you navigate to , you will find a JSON file filled with information about your current geolocation. My goal is to save this JSON data into JavaScript variables, allowing me to manipulate and extract specific fields from the file. ...

Convert the text inside a span element into a key-value object in JavaScript

How can I extract and save the text from a span element as key value pairs within a div? <div class="data-diff-span__composite-list-item__18c5zip data-diff-core__highlight-area__19c0zip"> <div class="data-diff-basic__class-row__4n ...

Automatically populate textboxes with database information based on selected combobox item without the need to refresh the page

I would like to implement a feature where textboxes are automatically filled from a database when a user selects an option from a combo box. This can be achieved using jQuery or JavaScript in PHP. Once a user selects an element, the corresponding text sh ...

Website navigation toolbar with access level control

I'm currently working on a website with various webpage links in the navigation menu, and I want to restrict access for non-admin users. Would it be better to set up different access levels or just use passwords on each page? What approach would be mo ...

NodeJS CORS functionality failing to function properly in the Google Chrome browser

In my nodejs script, I have implemented CORS as shown below: var express = require('express') , cors = require('cors') , app = express(); app.use(cors()); To fetch JSON data from another domain, I am using an AJAX request. While ...

Angular 2's Observable does not directly translate to a model

While delving into Angular 2, I experimented with utilizing an observable to retrieve data from an API. Here's an example of how I did it: getPosts() { return this.http.get(this._postsUrl) .map(res => <Post[]>res.json()) ...

Creating a single page application in Angular2+ using JSON data

I am looking to create an Angular JS application using the provided JSON structure: { name: 'App Name', pages: [ { name: 'Home', url: '/', layout: { type:'HTMLElement' tag:'div ...

Is there a method to update the status of a Jquery UI Mobile navbar using Javascript?

I have created a navigation bar using the Jquery mobile UI 1.01 framework; <div id="footer" data-role="footer" data-position="fixed"> <div data-role="navbar"> <ul> <li><a href="#" onClick="toggle_units();" clas ...

Unable to render $scope on the page

Upon querying a database, I am retrieving all the images associated with the user who is currently logged in. The code for this operation can be found in the uploadController: UserImage.get($scope.user_id) .success(function(data) { $scope.userA ...

Working with Ruby on Rails by editing a section of embedded Ruby code in a .js.erb file

Currently, I am in the process of developing a single-page website and have successfully implemented ajax loading of templates to insert into the main content section. However, I am encountering difficulties when trying to do this with multiple templates u ...

Troubleshooting problem with rotation and text geometry label on AxisHelper

Within my main scene, there is a sphere along with a subwindow located in the bottom right corner where I have displayed the (x,y,z) axis of the main scene. In this specific subwindow, I am aiming to label each axis as "X", "Y", and "Z" at the end of each ...

Sending props to a component without causing it to render(Note: This is

I am trying to figure out how to pass props from one component to another component without rendering a new component on the same page. I want to send the props selects from /memo to /memo/start using the function startMemo(). Code: Memo.js import React, ...

Disable the scroll animation feature for certain IDs

I implemented JavaScript code to animate scrolling for each block with a specific ID. However, when I added Bootstrap's tabs, the animation interfered with the functionality of the tabs. Is there a way to disable the scroll animation specifically for ...