How can call a method defined in child scope from its parent scope?
function ParentCntl() {     // I want to call the $scope.get here }  function ChildCntl($scope) {     $scope.get = function() {         return "LOL";         } }   http://jsfiddle.net/wUPdW/
child= {}; $scope. get = function(){ return $scope. child. get(); // you can call it.
In a nutshell: You cannot access child scopes from a parent scope. Your solutions: Define properties in parents and access them from children (read the link above) Use a service to share state.
Angular scopes include a variable called $parent (i.e. $scope. $parent ) that refer to the parent scope of a controller. If a controller is at the root of the application, the parent would be the root scope ( $rootScope ). Child controllers can therefore modify the parent scope since they access to it.
The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes.
You can use $broadcast from the parent to a child:
function ParentCntl($scope) {      $scope.msg = "";     $scope.get = function(){         $scope.$broadcast ('someEvent');         return  $scope.msg;             } }  function ChildCntl($scope) {                    $scope.$on('someEvent', function(e) {           $scope.$parent.msg = $scope.get();                 });      $scope.get = function(){         return "LOL";         } }   Working fiddle: http://jsfiddle.net/wUPdW/2/
UPDATE: There is another version, less coupled and more testable:
function ParentCntl($scope) {     $scope.msg = "";     $scope.get = function(){         $scope.$broadcast ('someEvent');         return  $scope.msg;             }      $scope.$on('pingBack', function(e,data) {           $scope.msg = data;             }); }  function ChildCntl($scope) {                    $scope.$on('someEvent', function(e) {           $scope.$emit("pingBack", $scope.get());             });      $scope.get = function(){         return "LOL";         } }   Fiddle: http://jsfiddle.net/uypo360u/
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