【发布时间】:2013-08-15 15:45:08
【问题描述】:
假设,我有 AngularJs 的以下代码:
angular.module('somename', []).
config(function ($routeProvider, $locationProvider) {
$routeProvider.
when('/test/:userid', { controller: TestDataCtrl }).
when('/users/:userid', {controller: UserDataCtrl}).
otherwise({redirectTo: '/index.html'});
$location.path('/newValue')
});
但是这个片段会抛出 ReferenceError: $location is not defined from archivarius。好的,没问题。我在函数中添加了第三个参数 $location 并得到了错误 -- Unknown provider: $location from archivarius。
那么,我怎样才能获得 $location 服务的实例呢?
换句话说,我有一些丑陋的网址,例如:
http://localhost:7000/myservice/1?html=true#/index
我上面的路线不起作用,因为它应该 该服务网址看起来像
http://localhost:7000/myservice/#/index
or
http://localhost:7000/myservice/index
但由于我想出了 REST 服务的内部重定向逻辑 AngularJs 的 url 有点令人困惑,我需要在不重新加载页面的情况下更改它。
提前致谢。
【问题讨论】:
-
为什么需要
$location.path('/newValue')? -
更改网址。我从例子中得到它......
-
您不会更改配置中的位置。您通常会在注入 $location 的控制器中执行此操作。
-
@Sharondio:我如何在页面加载时(或页面加载后)执行控制器?抱歉这些问题——我是 AngularJs 和 javascript 的新手。
-
每次路由改变时都会加载控制器。
标签: javascript angularjs