Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:12:33+00:00 2026-06-09T22:12:33+00:00

I have two Angular controllers: function Ctrl1($scope) { $scope.prop1 = First; } function Ctrl2($scope)

  • 0

I have two Angular controllers:

function Ctrl1($scope) {
    $scope.prop1 = "First";
}

function Ctrl2($scope) {
    $scope.prop2 = "Second";
    $scope.both = Ctrl1.prop1 + $scope.prop2; //This is what I would like to do ideally
}

I can’t use Ctrl1 inside Ctrl2 because it is undefined. However if I try to pass it in like so…

function Ctrl2($scope, Ctrl1) {
    $scope.prop2 = "Second";
    $scope.both = Ctrl1.prop1 + $scope.prop2; //This is what I would like to do ideally
}

I get an error. Does anyone know how to do this?

Doing

Ctrl2.prototype = new Ctrl1();

Also fails.

NOTE: These controllers are not nested inside each other.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:12:35+00:00Added an answer on June 9, 2026 at 10:12 pm

    One way to share variables across multiple controllers is to create a service and inject it in any controller where you want to use it.

    Simple service example:

    angular.module('myApp', [])
        .service('sharedProperties', function () {
            var property = 'First';
    
            return {
                getProperty: function () {
                    return property;
                },
                setProperty: function(value) {
                    property = value;
                }
            };
        });
    

    Using the service in a controller:

    function Ctrl2($scope, sharedProperties) {
        $scope.prop2 = "Second";
        $scope.both = sharedProperties.getProperty() + $scope.prop2;
    }
    

    This is described very nicely in this blog (Lesson 2 and on in particular).

    I’ve found that if you want to bind to these properties across multiple controllers it works better if you bind to an object’s property instead of a primitive type (boolean, string, number) to retain the bound reference.

    Example: var property = { Property1: 'First' }; instead of var property = 'First';.


    UPDATE: To (hopefully) make things more clear here is a fiddle that shows an example of:

    • Binding to static copies of the shared value (in myController1)
      • Binding to a primitive (string)
      • Binding to an object’s property (saved to a scope variable)
    • Binding to shared values that update the UI as the values are updated (in myController2)
      • Binding to a function that returns a primitive (string)
      • Binding to the object’s property
      • Two way binding to an object’s property
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data here: The result of this first function: Array ( [2]
I have two update panels on a page. And I want to update both
I have two queries that find both the Zip codes, and the States for
I have two classes Order and Items I want a method like this class
I have two Hibernate data object. The first is a User (with unique id,
Have two events: $('body').mouseup(function(e){} and $('.toggle').click(function(e){} I only want one of these to trigger.
I have two strings like this $text = 'Abcdef'; $text[7] = 'Ghijk'; When I
Have two fields start and end, both are dates, I want to write a
I have two collections, the first with an array of id's referencing documents in
Have two variables both containing integers: var input; var value; Need to perform arithmetic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.