AngularJS $ routeParams未定义

时间:2015-10-14 03:02:41

标签: angularjs

我无法从控制器访问 $ routeParams 值。

我的角度应用程序定义如下;

(function () {
    'use strict';

    var app = angular.module("rbApp", [
        "ngAnimate", "ui.router", "ui.bootstrap", "ngMessages", "ngRoute", "common.services"    ]);

    app.config(["$stateProvider", "$urlRouterProvider", "$locationProvider", "$urlMatcherFactoryProvider", function ($stateProvider, $urlRouterProvider, $locationProvider, $urlMatcherFactoryProvider) {
        $urlMatcherFactoryProvider.caseInsensitive(true);
        $urlMatcherFactoryProvider.strictMode(false);
        $urlRouterProvider.otherwise("/");

        $stateProvider
        .state("confirmemail", {
            url: "/Confirm_Email?userId",
            templateUrl: "ceForm.html",
            controller: "ceController as ceVm"
        });
    }]);
})();

我的控制器是;

(function () {
    "use strict";

    angular
        .module("common.services")
        .controller("ceController", ["$scope", "$state", "$routeParams", "pageContentService", "accountService", "sharedServices", ceController]);

    function ceController($scope, $state, $routeParams, pageContentService, accountService, sharedServices) {
        var vm = this;
        vm.emailConfirmed = false;

        console.log("confirmEmail userId: ", $routeParams.userId);

        if ($routeParams.userId) {
            .
            .
            .
        }

    };
})();

但是当我传入像http://example.com?userId=9daa2c41 这样的网址时,$ routeParams.userId 总是未定义。

1 个答案:

答案 0 :(得分:0)

您必须在<ng-view>中加载视图才能获得$routeParams。由于$routeParams$route相关联,因此在未使用路由时它没有值。如果你要走这条路,你的$stateParams同样适用。