Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $routeParams未定义。我不确定是什么让它突然失效了_Angularjs_Angularjs Routing - Fatal编程技术网

Angularjs $routeParams未定义。我不确定是什么让它突然失效了

Angularjs $routeParams未定义。我不确定是什么让它突然失效了,angularjs,angularjs-routing,Angularjs,Angularjs Routing,我不知道这里发生了什么事 angular.module('myApp', [ 'ngRoute', 'myApp.controllers', 'myApp.filters', 'myApp.services', 'myApp.directives' ]). config(function ($routeProvider, $locationProvider) { $r

我不知道这里发生了什么事

 angular.module('myApp', [
        'ngRoute',
        'myApp.controllers',
        'myApp.filters',
        'myApp.services',
        'myApp.directives'
    ]).
        config(function ($routeProvider, $locationProvider) {
            $routeProvider.
                when('/', {
                    templateUrl: '/partials/homepage',
                    controller: 'MyCtrl1'
                }).
                when('/about/:id', {
                    templateUrl: '/partials/'+$routeParams.id,
                    controller: 'MyCtrl1'
                }).
                when('/funnel', {
                    templateUrl: '/partials/funnel',
                    controller: 'MyCtrl2'
                }).
                otherwise({
                    redirectTo: '/'
                });

            $locationProvider.html5Mode(true);
        });

无论我浏览到/或/关于/:id,我得到$routeParams是未定义的。

这是因为这一行:

templateUrl: '/partials/'+$routeParams.id,
您使用的是
$routeParams
,而不是每次声明或注入它。如果将其添加到函数接受的参数中,则页面应停止抛出该错误:

config(function ($routeProvider, $routeParams, $locationProvider) {
    // ...
}

您需要注入
$routeParams
服务才能使用它。但是,在您的情况下,似乎希望基于routeparam动态确定模板。您不能在应用程序的配置阶段直接执行此操作,该阶段仅作为应用程序初始化阶段的一部分运行一次(并且您还可以在应用程序的配置阶段注入
$routeParams
,因为没有此类提供程序)。您可能需要寻找一种检索动态模板的方法,为了支持此功能,您可以使用函数as
templateUrl
,以便能够基于任何routeparameters(将作为函数中的参数)动态确定模板url

您可以这样做:-

           when('/about/:id', {
                templateUrl: function(routeParam){ //register it as function
                    return '/partials/' + routeParam.id;  //Get the id from the argument
                },
                controller: 'MyCtrl1'
            }).
就从

templateUrl–{string=| function()=}–返回ngView应使用的html模板路径的路径或函数。 如果templateUrl是一个函数,则将使用以下参数调用它:
{Array.}
-通过应用当前路由,从当前$location.path()提取路由参数


你到底想干什么?动态模板URL?是的。我已经包括了ngRoute,但由于某些原因,该变量没有被识别,甚至在我浏览主页时也会抛出一个错误。如果我在配置中添加$routeParams,我会得到错误:[$injector:unpr]未知提供程序:$routeParams这对我有效。我发现了很多不同的方法,这有点让人困惑。谢谢