I need to call method for initialization user setting values (which are set to rootscope) from second cotroller in method of the first controller.
After the values are succesfully set to rootscope return premise and continue in executing of the second method of the first controller.
I tried it with emit and broadcast examples, but without luck.
Could somebody please give me a advice how to do it correctly?
I am on Angular 1.2 and Ionic 1, beta 13
Many thanks for any help.
Template:
<ion-view title="{{ 'DAYS_RESULTS' | translate }}" >
<ion-content ng-controller="DailyStatsCtrl" ng-init="setData()">
Code:
// First cotroller call setData in ng-init
angular.module('starter')
// Controller definition
.controller('DailyStatsCtrl', function($scope, $rootScope, $ionicSlideBoxDelegate, $timeout , $ionicLoading , $q, $translate, DialsComputeService, cordova, LocalStorService, $ionicPopup, $state, $ionicNavBarDelegate, testService, $ionicPlatform) {
$scope.setData = function() {
$ionicPlatform.ready(function() {
$timeout(function() {
$scope.$emit("onSomething", "");
alert("TEST");
}, 1000);
});
};
In the second controller:
angular.module('starter')
// Controller definition
.controller('SettingsCtrl', function($scope, $rootScope, $ionicLoading, $ionicPlatform, LocalStorService, $timeout, $translate, $ionicPopup, $state, $ionicNavBarDelegate) {
$scope.getUserSettigns = function() {
alert("received");
$scope.test = 'event received';
}
$scope.$on('onSomething', function(e) {
$scope.getUserSettigns();
});
To achive this you don't really need to use
$rootScope(but you can),$broadcastwill call watchers for event in current and all child scopes, so this code in parent controller:will trigger all
$scope.$on('onSomething', ..)callback in current and all child controllers.here's a codepin with working example(text 'event received' will appear after 5s in rendered html).
http://codepen.io/anon/pen/VYeEOg