angularjs从外部角度重定向
问题描述:
我有一个NG-应用和NG-视图。该应用程序有多个控制器。
I have an ng-app and an ng-view. The app has multiple controllers.
从外界的角度,传统的JS code,我想重定向一定的控制器。
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.
答
$位置
是角服务,它不是,除非你加它的地方$范围的属性(其他)。要获得 $位置
您的应用程序之外,你可以使用 $注射器
服务 - 这样的:
$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");