从角色的url()获取Id

时间:2017-06-08 05:43:07

标签: angularjs

如何从网址()获取url这是我的http://localhost:59113/Project/EditProject?id=2

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

我是用jquery做的..

 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)
                }])

但是我需要使用angular来做。任何人都可以帮助我吗?我也试过了(更新的部分)

Uncaught SyntaxError: Unexpected token import
    at Object.<anonymous> (bundle.js:3391)
    at __webpack_require__ (bundle.js:556)
    at fn (bundle.js:87)
    at Object.eval (eval at <anonymous> (bundle.js:1405), <anonymous>:11:25)
    at Object.eval (eval at <anonymous> (bundle.js:1405), <anonymous>:24:27)
    at eval (eval at <anonymous> (bundle.js:1405), <anonymous>:25:30)
    at Object.<anonymous> (bundle.js:1405)
    at __webpack_require__ (bundle.js:556)
    at fn (bundle.js:87)
    at Object.eval (eval at <anonymous> (bundle.js:3379), <anonymous>:12:17)

3 个答案:

答案 0 :(得分: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'
      })
})

答案 1 :(得分:0)

$ location.search()返回一个对象,如上面的url

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

$ location.search()。id将为您提供答案。

更多信息:

如果您使用$ location.search()获取空对象,可能是因为Angular正在使用hashbang策略,要修复此问题,您需要在配置文件中更改以下内容

$locationProvider.html5Mode(true);

答案 2 :(得分:0)

$location.search()将返回键值对的对象。 在您的情况下:您可以使用$location.search().id

直接访问此值