Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone provide a use case for the $controller service in AngularJS?

Angularjs docs give the usage of $controller service as: $controller(constructor, locals);

Can anyone focus some light on these 2 points:

  1. When to use $controller service. Please provide some use case.
  2. Details about 'locals' parameter passed to it.
like image 246
mia Avatar asked Jan 09 '15 18:01

mia


People also ask

What method is used to implement a service in AngularJS?

Services are normally injected using the dependency injection mechanism of AngularJS.

Which function is used to create custom service in AngularJS?

Services are normally injected using dependency injection mechanism of AngularJs. Actually angular js provides many inbuilt services for our uses, eg. $http, $route, $location etc. Each service is responsible for a specific work as example , $http is used for make ajax request call to get or post the server data.

What are the differences between AngularJS module's service provider and factory?

provider. Providers have the advantage that they can be configured during the module configuration phase. See here for the provided code. So factory is a function which is responsible for creating the value.


2 Answers

You can create common functions which are to be executed on $scope into one controller may be named 'CommonCtrl'.

angular.module('app',[]).controller('CommonCtrl', ['$scope', function($scope){
      var self = this;
      $scope.stuff1 = function(){

      }

      $scope.stuff2 = function(){

      }
      self.doCommonStuff = function(){
               // common stuff here
               $scope.stuff1();
               $scope.stuff2();

      };
      return self;
}]);

And inject this controller in other controllers let say 'TestCtrl1' like

angular.module('app',[]).controller('TestCtrl1', ['$scope','$controller', function($scope, $controller){
        var commonCtrl = $controller('CommonCtrl',{$scope: $scope}); // passing current scope to commmon controller
        commonCtrl.doCommonStuff();
}]);

Here, the in second argument of $controller service, we are passing dependencies that are required by CommonCtrl. So the doCommonStuff method will use TestCtrl1 controller's scope.

like image 98
Shripal Soni Avatar answered Oct 30 '22 06:10

Shripal Soni


To mention one, it is useful in creating the target controller during unit testing.

Lets say you have a controller with signature .controller('MainCtrl', function($scope, serviceA){..}).

In testing,

// ...

beforeEach(inject(function ($rootScope, $controller, serviceA) {

  // assign injected values to test module variables
  scope = $rootScope.$new();
  service = serviceA

  // create the controller, by passing test module variables values as dependencies
  $controller('MainCtrl', {'$scope': scope, 'serviceA': service});
}));

it('test on controller', function() {
  //...
});

For more info checkout: https://docs.angularjs.org/guide/unit-testing

like image 36
Vamshi Suram Avatar answered Oct 30 '22 05:10

Vamshi Suram