-2

I have a parent controller and some child controllers in my app. In some cases i need to redirect user to another route url, and call the function after redirect (in url controller).

<div ng-controller="parent">
  <!-- ng-view below -->
  <div ng-controller="url1"></div> <!-- #/url1 -->
  <div ng-controller="url2"></div> <!-- #/url2 -->
 </div>

when user still in #/url1 in a particular case i need to redirect to #/url2 and then call a function in url2 controller.

trigger
  • 489
  • 10
  • 26
  • Its really not a redirection to a page. – Bhojendra Rauniyar Jul 27 '15 at 08:44
  • You need to read the [Angular docs](https://docs.angularjs.org/api) because most of us will not write the code for you. Read up on controllers. Also, have a look at [Angular UI-Router](https://github.com/angular-ui/ui-router) – Dr G. Jul 27 '15 at 08:45

1 Answers1

0

If you only want to use a function in another controller, you can inject that controller into your current controller and then call the function you want using the injected controller.

Check this out for how to get this done.

This way, you will not be changing the routes as well.

Community
  • 1
  • 1
v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • Thank you, but can i use smth like $location.path('new_url'); $scope.$on('$routeChangeSuccess', function (scope, next, current) { $rootScope.$broadcast('openFunction', note); }); – trigger Jul 27 '15 at 08:59
  • It is not a good practise to use the rootScope of an Angular application. I suggest that injecting the controller is a better way. Or you can move that function into a service and use it by injecting the Service in all your controllers where you will be using the function. – v1shnu Jul 27 '15 at 09:03