從角

2017-06-08 15 views
2

URL()獲取標識如何從URL id()
這是我url從角

http://localhost:59113/Project/EditProject?id=2 

我做到了使用jQuery ..

var url = document.URL; 
     var id = /id=([^&]+)/.exec(url)[1]; 
     var result = id ? id : ' '; 

但我需要做的它使用angular.Can任何人都可以幫助我嗎?
我想這太(更新部分)

var app = angular 
       .module("intranet_App", []) 
        app.config("$routeProvider", function ($routeProvider) { 
         $routeProvider.when('/view1/:param1/:param2', { 
          templateUrl: 'Project/EditProject.html', 
          controller: 'myCtrl' 
         }) 
        }) 
        .controller("myCtrl", ["$scope", "$http", "$location", "$route", function ($scope, $http, $location, $route) { 
        // alert($location.id) 
        var a = $route.current.params; 
        alert(a.id) 
       }]) 
+0

我試過這個'console.log($ location.search().id)'它顯示未定義。 – user7397787

+0

嘗試'console.log($ location.id)' –

+0

@Hadi yes試過..沒有來臨 – user7397787

回答

3

使用$routeParams,你可以得到的ID,

更新答案

使用$route而不是$routeParams。請注意,$ routeParams僅在路由更改成功完成後纔會更新。這意味着您不能依賴$ routeParams在路由解析函數中正確。相反,您可以使用$ route.current.params來訪問新的路由參數。

$route.current.params 

編輯

必須這樣做,在.config()方法

app.config(function($routeProvider){ 
     $routeProvider.when('/view1/:param1/:param2', { 
      templateUrl: 'Project/EditProject.html', 
      controller: 'myCtrl' 
     }) 
}) 
+0

立即檢查並讓我知道 –

+0

您忘記在依賴注入中包含'$ routeProvider' –

+0

不,請再次檢查我的答案。 'app.config(函數($ routeProvider){'然後你用'a.id'得到id –

0

$ location.search()返回一個對象,所以按照上面的URL

http://localhost:59113/Project/EditProject?id=2 

$ location.search().id會給你一個答案。

更多信息:

如果你正在使用$ location.search()一個空的對象,很可能是因爲角度使用hashbang戰略,要解決這個問題,你需要更改配置文件

以下
$locationProvider.html5Mode(true); 
0

$location.search()將返回鍵值對的對象。你可以直接訪問這個值$location.search().id