0

I have two controllers like below. I want to use first controller methods/variables in other controller

app.controller("createController", ["$scope",
 function ($scope)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    console.log('changeEstimateStatus');
  };
 }]);


 app.controller("editController", ["$scope",
 function ($scope)
 {
    //here how can I access 'i' variable of createController
 }]);
4
  • How are the controllers related? Parent/child - separate pages? Give more info!\ Commented Oct 30, 2014 at 13:07
  • 2
    seems like the place to use a service which has the variable i and then inject it to the controllers Commented Oct 30, 2014 at 13:09
  • 1
    Seperate pages..please suggest without using $rootScope Commented Oct 30, 2014 at 13:09
  • 1
    seems like the place to use a service which has the variable i and then inject it to the controllers – getting console error Commented Oct 30, 2014 at 13:35

3 Answers 3

3

Use a shared service:

app.service("mySharedService", [function() {
    var _x = null;

    return {
        setX: function(val) { _x = val },
        getX: function() { return _x }
    }
}]);

Then inject into your controller:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) {

    $scope.i = mySharedService.getX(); //get
    mySharedService.setX(3); //set
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thnx alot for your valuable information tyme. If I have more methods & variables, please suggest any other solution...
0

use one of this:

1.you can use serveice and put common function to that service and access that function to all controlller.

app.service('MyService', function() {
     this.changeEstimateStatus = function()
      {
        console.log('changeEstimateStatus');
      };
});

   app.controller("createController", ["$scope",MyService,
     function ($scope,MyService)
     {
      $scope.i = 0;
     MyService.changeEstimateStatus ();
     }]);


     app.controller("editController", ["$scope",   app.controller("createController", ["$scope",$rootScope,
     function ($scope,$rootScope)
     {
      $scope.i = 0;
      MyService.changeEstimateStatus ();

     }]);

2.you can store that function in $rootscope object.and then access that function to all controlller.

like:

app.controller("createController", ["$scope",$rootScope,
 function ($scope,$rootScope)
 {
  $scope.i = 0;

 }]);


 app.controller("editController", ["$scope",$rootScope,
 function ($scope,$rootScope)
 {
    $rootScope.changeEstimateStatus ();
 }]);

but 2nd option is not a good aproach.

4 Comments

I agree about using a service. It'd be nice to know why though. In my opinion it's better to use a service because then you've got to explicitly import that dependency where you need it. If you use the root scope that method will be available everywhere.
@BenCr A service is always the correct place to put code that should be shared among multiple controllers, which is exactly what OP wants. $rootScope should be avoided whenever possible, because it's shared with EVERY piece of the app, while a service can be injected only where needed.
Is that not exactly what I said? The point I was making is that just saying "this isn't a good approach" without telling someone why doesn't make for a high quality SO answer.
You should never write methods that are automatically available everywhere, because then you might trip on them unexpectedly. It's the exact same reason you shouldn't use global variables in JavaScript.
0

You should move the common functionality (changeEstimateStatus) into a service. Rather than one controller depending on the other, both controllers depend on the service.

app.service('estimateService', function() {

  this.changeEstimateStatus = function()  {
    console.log('changeEstimateStatus');
  };

});
    app.controller("createController", ["$scope","estimateService",
 function ($scope, estimateService)
 {
  $scope.i = 0;
  $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);


 app.controller("editController", ["$scope", "estimateService"
 function ($scope, estimateService)
 {
    $scope.changeEstimateStatus = function()
  {
    estimateService.changeEstimateStatus(); //delegate to the service
  };
 }]);

Another option is to use $rootScope, but using services is not as brittle to change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.