$emit is way better that $broadcast : You can call controllers like following code. You can send data as well. Try this .
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click="sendData();"></button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $rootScope) {
function sendData($scope) {
var arrayData = [1,2,3];
$rootScope.$emit('someEvent', arrayData);
}
});
app.controller('yourCtrl', function($scope, $rootScope) {
$rootScope.$on('someEvent', function(event, data) {
console.log(data);
});
});
</script>
or the simplest but not recommended way :
<div ng-app="myApp">
<div class="div1" ng-controller="ControllerA" ng-show="tab1" ng-click="changeTab('tab1')">
...
</div>
<div class="div2" ng-controller="ControllerB" ng-show="tab2" ng-click="changeTab('tab2')">
...
</div>
<div class="div3" ng-controller="ControllerC">
...
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.run(function($rootScope) {
$rootScope.tab1 = true;
$rootScope.tab2 = false;
});
app.controller('ControllerA', function($scope, $rootScope) {
$scope.changeTab = function (tab) {
$rootScope.tab1 = true;
$rootScope.tab2 = false;
}
});
app.controller('ControllerB', function($scope, $rootScope) {
$scope.changeTab = function (tab) {
$rootScope.tab1 = false;
$rootScope.tab2 = true;
}
});
app.controller('ControllerC', function($scope, $rootScope) {
});
</script>
$rootScope.$broadcast