Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs redirect from outside angular

I have an ng-app and an ng-view. The app has multiple controllers.

From outside angular, legacy JS code, I'd like to redirect a certain controller.

var e = document.getElementById('mApp');
var scope = angular.element(e).scope();

scope.$apply(function() { scope.$location.path("/account/login"); });

I've tried $scope.$location and it's telling me $location is undefined so I must be doing something wrong.

like image 447
MB. Avatar asked Dec 01 '22 19:12

MB.


1 Answers

$location is an Angular service, it's not a property of $scope (unless you add it somewhere else). To get $location outside of your app you could use the $injector service - like this:

var e = document.getElementById('mApp');
var $injector = angular.element(e).injector();

var $location = $injector.get('$location');
$location.path("/account/login");
like image 120
joakimbl Avatar answered Dec 04 '22 07:12

joakimbl