I have a directive: in template:
<territorial-selector></territorial-selector>
in js:
app.directive('territorialSelector', function() {
return {
restrict: 'E'
,templateUrl: 'partials/territorial-selector.html'
,controller: 'TerritorialSelectorController'
};
};
As you can see the directive use "TerritorialSelectorController"
In another one place I have a button that should execute method loadTerritories() from TerritorialSelectorController. I created this button:
<button ng-controller="TerritorialSelectorController" ng-click="loadTerritories()">BUTTON</button>
The problem that in this case TerritorialSelectorController creates two times. Here is the code of TerritorialSelectorController:
app.controller('TerritorialSelectorController', ['$scope', function($scope) {
alert('AAAAA'); // I have alert two times
}]);
And I need only one object of the controller.
AngularJS services are: Lazily instantiated – AngularJS only instantiates a service when an application component depends on it. Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.
Yes mate, factories and a services are both Singletons.
Services are singleton objects which are instantiated only once in app and are used to do the defined task.
In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application. AngularJS has about 30 built-in services. One of them is the $location service.
Next to creating controllers and directives, AngularJS also supports “singleton” services. Services, like on the server-side, offer a great way for separating logic from your controllers. In AngularJS anything that’s either a primitive type, function or object can be a service.
AngularJS will invoke the controller with a $scope object. In AngularJS, $scope is the application object (the owner of application variables and functions). The controller creates two properties (variables) in the scope (firstName and lastName). The ng-model directives bind the input fields to the controller properties (firstName and lastName).
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object. In AngularJS, $scope is the application object (the owner of application variables and functions).
To use an AngularJS service, you add it as a dependency for the component (controller, service, filter or directive) that depends on the service. AngularJS's dependency injectionsubsystem takes care of the rest.
If you want the functionality to work as a singleton, then you will need it to be inside a service that your controllers then access. Controllers are not singletons; you can have many, many instances of the same basic controller in your code.
If you instead create a service, then the common data/functionality that each controller must share can be placed, and accessed, through that service.
Here's a Plunk demo that shows two controllers sharing data through a service. It's not two controllers of the same type, but it will show you the basics of how services work.
Here's some code from the demo, as well. Controller:
app.controller('SunListCtrl', function($scope, List, $log) {
$scope.items = List.getList();
$scope.$on('updatedList', function() {
$scope.items = List.getList();
$log.info('List has been updated. Size = ' + $scope.items.length);
$log.info('\tNow set to: ' + $scope.items);
});
});
Service:
app.service('List', function($rootScope, $log) {
service = {}; // the service object we'll return
var dateValue = 'date';
var geoValue = 'geo';
var theList = [];
service.setGeo = function(data) {
geoValue = data;
updateList();
}
service.setDate = function(data) {
dateValue = data;
updateList();
}
function updateList() {
theList = [dateValue, geoValue];
$rootScope.$broadcast('updatedList'); // broadcasts the update to listeners
}
service.getList = function() {
return theList;
}
return service;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With