2014-06-06 65 views
0

我是angularJs的新手。我正在創建一個應用程序,我在URL中傳遞用戶標識,然後嘗試使用Routeparam獲取Js文件中的標識,但它不工作。我正在使用MVC 4和angularjs。 請參閱我的代碼angularjs routePrams不工作

我的網址/首頁/ GetUserInformation?userID = 44。我想在下面的代碼中的用戶ID

var userInformationApp = angular.module('UserInformationApp', ['ngResource']) 
    .config(function config($routeProvider, $stateProvider) 
    { 
    $locationProvider.html5Mode(true).hashPrefix('!'); 
     $routeProvider. 
      when('/Home/GetUserInformation?userID=:ID', 
      { 
       templateUrl: "UserInformation.cshtml", 
       controller: "UserInformationController" 

      }) 


    angular.module('UserInformationApp', []).controller("UserInformationController", function ($scope, $routeParams, $http) 
    { 
     debugger;-----// My debugger is not hitting when I used routeParams.If I remove Route params Then it is working fine but route params then come undefined. 

     $scope.saveNote = function() 
     { 
      $scope.newNote = $scope.userNote; 
      $scope.intialValue = true; 
      $scope.record = $routeParams.ID;///// This I saw in net to get the ID or information from the URL in angular js. 

     } 

    }); 

當我使用RouteParams我的調試器沒有打。

回答

0

有一個URL獲取參數的方法有兩種:用$routeParams和手動。

角方式

在角的正常方式是$routeParams

when('/Home/GetUserInformation/:userid', 

,現在,它在控制器可用:

$scope.record = $routeParams.userid 

有些教程

查詢字符串

另外,您也可以手動得到的查詢字符串參數。

如果你知道了參數的名稱:

$location.search().yourparam 

,如果你有任意數目參數的,你不知道他們的名字(例如,以初始化應用程序的設置):

var queryString = $location.absUrl().match(/^[^?]+\??([^#]*).*$/)[1]; 
var items = queryString.split("&"); 
var params = {}; 

for (var i = 0, l = items.length; i < l; i++) { 
    var temp = items[i].split("="); 
    if (temp[0]) { 
     if (temp.length < 2) { 
      temp.push(""); 
     } 
     params[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]); 
    } 
} 
+0

不working.Actually當我使用$ routeParam在控制我的流程breaking.It沒有控制器功能 – user3714087

+0

你可以試試,在你的控制器聲明中去,換款'angular.module(「UserInformationApp」,[]) 'for angular.module('UserInformationApp')'? 「我的流量突破」不是錯誤消息。你也可以閱讀文檔https://docs.angularjs.org/api/ngRoute/service/$routeParams –