admin管理员组文章数量:1353497
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.
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.
Share Improve this question edited May 7, 2015 at 19:50 Jerald asked May 7, 2015 at 19:39 JeraldJerald 4,05811 gold badges50 silver badges84 bronze badges 5- 2 I guess you should create Angular service instead - docs.angularjs/guide/services – tiblu Commented May 7, 2015 at 19:42
-
1st place what do you mean by
Singleton controller
? – Pankaj Parkar Commented May 7, 2015 at 19:42 - I mean that I don't want two objects of TerritorialSelectorController. – Jerald Commented May 7, 2015 at 19:46
- service/factory/provider are singleton in angular..do go for it as its already suggested by @tiblu – Pankaj Parkar Commented May 7, 2015 at 19:53
-
If you ever hear yourself say the word
singleton
in the context of Angular, think services not controllers. – David says Reinstate Monica Commented May 7, 2015 at 19:55
1 Answer
Reset to default 11If 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 mon 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;
});
本文标签: javascriptSingleton controller in AngularJsStack Overflow
版权声明:本文标题:javascript - Singleton controller in AngularJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743932841a2564159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论