Angularjs redirects externally angular

I have an ng application and ng-view. The application has several controllers.

From the outside, angular, legacy JS code, I would like to redirect a specific controller.

var e = document.getElementById('mApp'); var scope = angular.element(e).scope(); scope.$apply(function() { scope.$location.path("/account/login"); }); 

I tried $ scope. $ location, and he told me that $ location is undefined, so I have to do something wrong.

+4
source share
2 answers

$location is an Angular service, it is not a $ scope property (unless you add it to another location). To get $location outside your application, you can use the $injector service - for example:

 var e = document.getElementById('mApp'); var $injector = angular.element(e).injector(); var $location = $injector.get('$location'); $location.path("/account/login"); 
+14
source

This is how I solved it. This will allow you to redirect to angular from outside angular.

 $('html').injector().get('$location').path("/where/you/want/to/go"); $('html').scope().$apply(); 

$('html') should be your root angular element (where the ng application is installed)

+4
source

Source: https://habr.com/ru/post/1479823/


All Articles