简体   繁体   English

AngularJS $ locationProvider.search()导致未捕获对象错误

[英]AngularJS $locationProvider.search() results in Uncaught Object error

In AngularJS I get an error when attempting to use the $locationProvider to parse the query string. 在AngularJS中,尝试使用$ locationProvider解析查询字符串时出现错误。 The AngularJS Docs say that locationProvider hasn't moved from the core, so I'm wondering why the code below doesn't work. AngularJS Docs说locationProvider并没有脱离核心,所以我想知道为什么下面的代码不起作用。 It sets html5Mode fine, but it's like the search method doesn't exist. 它将html5Mode设置得很好,但是这就像不存在搜索方法。

var myApp = angular
    .module('agencyMatrix', ['ngRoute']);

myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(false);
    var location = $locationProvider.search();
}

search() is actually defined on $location , not $locationProvider . search()实际上是在$location而不是$locationProvider上定义的。

See the $location documentation and the source: angular.js/src/ng/location.js 请参阅$location文档和源: angular.js / src / ng / location.js

To use $location.search you can access it inside run instead of config: 要使用$location.search您可以在run而不是config中访问它:

var myApp = angular
    .module('agencyMatrix', ['ngRoute']);

myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(false);
}]).run(['$location', function($location) {
    var location = $location.search();
}])

The providers guide from the angular documentation (specifically, the Provider Recipe section) describes the configuration phase and how it is different from the other phases (for example, $location itself is unavailable during the configuration phase). 角度文档中的提供者指南(特别是“ 提供者食谱”部分)描述了配置阶段及其与其他阶段的区别(例如,在配置阶段$location本身不可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM