2015-10-14 86 views
0

我無法從我的控制器訪問$ 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) { 
      . 
      . 
      . 
     } 

    }; 
})(); 

但是當我在URL傳似http://example.com?userId=9daa2c41$ routeParams.userId永遠是不確定的。

+4

您正在使用ui路由器,因此正確的提供程序是'$ stateParams'。 '$ routeParams'屬於'ngRoute'模塊。我不會推薦混合兩個 – Phil

+0

謝謝@Phil,解決了我的問題。 –

回答

0

您必須在<ng-view>中加載您的視圖才能獲得$routeParams。由於$routeParams$route相關聯,因此在未使用路由時它不具有值。如果你要去那條路線,同樣的事情適用於你的$stateParams