简体   繁体   English

AngularJS路由,如何通过视图URL传递参数

[英]AngularJS routing , how to pass parameter with a url of view

i want to pass parameter id with url in angularJS , i try in home.config : 我想在angularJS中传递带有URL的参数id ,我尝试在home.config中

home.config(["$routeProvider",function ($routeProvider) {
$routeProvider
    .when("/profile",{
        templateUrl : "views/home/profile.html",
        controller  : "profile"
    })
    .when('/settings',{
        templateUrl : "views/home/settings.html",
        controller  : "Settings"
    })
    .when('/item',{
        templateUrl : "views/home/item.html",
        controller  : "item"
    });

}]); }]);

in home.html : home.html中

<a href="#!item?id='+Item_id+'" >Item View</a>

and in itemController 并在itemController中

controllers.controller("item",function ($scope,$http,$rootScope) {
 var url_string = window.location.href; // home.php#!/item?id=0ae8b2fc-3ccb-11e8-952b-708bcd9109ce
 var url = new URL(url_string);
 var item_id = url.searchParams.get("id");
 console.log(item_id);
})

but I get a null value, please help thanks in advance . 但我得到一个null值,请提前帮助谢谢。

If you are using ngRoute you should use $route in the controller to get the parameters of the route. 如果您使用的是ngRoute,则应在控制器中使用$ route获取路由的参数。

You can see working code here: 您可以在此处查看工作代码:

const app = angular.module("mainapp", ["ngRoute"]);
app.config(["$routeProvider", function($routeProvider) {
  $routeProvider
    .when("/profile", {
      template: "<p>profile</p>",
      controller: function() {}
    })
    .when('/settings', {
      template: "<p>settings</p>",
      controller: function() {}
    })
    .when('/item', {
      template: "<p>item {{itemid}}</p>",
      controller: function($route, $scope) {
        $scope.itemid = $route.current.params.itemid;
      }
    });
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM