JSFiddle - React, Tailwind, and code Playground
HTML
<div ng-app="MY_APP">
<div ng-controller="MyCtrl1"> Controller1 - parent
<div ng-controller="MyCtrl2"> Controller2 - Child
</div>
</div>
</div>
JavaScript
var app = angular.module("MY_APP", []);
//Parent Controller
app.controller("MyCtrl1", [
'$scope','$q','$timeout', function($scope,$q,$timeout) {
$scope.async = function(name) {
var deferred = $q.defer();
//Async call: Use your ajax call here instead of $timeout
/*
$http.get('/someUrl')
.success(function(data) {
$scope.myVar = 'xyz';
deferred.resolve(data);
}).error(function(msg, code) {
deferred.reject(msg);
$log.error(msg, code);
});
*/
$timeout(function() {
deferred.notify('About to greet ' + name + '.');
if (name) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting ' + name + ' is not allowed.');
}
}, 1000);
return deferred.promise;
}
}]);
//Child Controller
app.controller("MyCtrl2", [
'$scope','$q', function($scope,$q) {
// call parent async method
var promise = $scope.$parent.async('Sai');
promise.then(function(greeting) {
//check your variable here
/*
console.log($scope.$parent.myVar);
*/
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
}]);