【问题标题】:How to pass URL parameter to controller如何将 URL 参数传递给控制器
【发布时间】:2017-02-07 01:02:28
【问题描述】:

我需要将 URL 参数传递给控制器​​。在下面的 URL 中,我需要将 ID 参数传递给控制器​​。

http://localhost/branch/more.php?id=20

ID URL 参数传递给我在下面给出的控制器。

  app.controller('customersCtrl', function($scope, $http) {
  $http.get("message.php?id=").then(function (response) {
  $scope.myData = response.data.message;
  });

所以,message.php?id=Parameter ID Here一定是这样的。

【问题讨论】:

  • 为什么要删除答案?

标签: angularjs


【解决方案1】:

在你的控制器中注入 $routeParams 就像

 app.controller('customersCtrl', function($scope,$http,$routeParams) {
 var routeID = $routeParams.id;
 $http.get("message.php?id="+routeID).then(function (response) 

【讨论】:

  • 为什么要删除答案?
  • 添加 $routeParamsvar routeID = $routeParams.id; 后,整个应用程序停止工作。
  • 我在 Mozilla 开发者工具中检查了 Network,它没有从 message.php 中获取任何结果。
  • 但这就是你从 routerparams 获取 id 的方式
  • 问题是,当我删除 $routeParamsvar routeID = $routeParams.id; 时,应用程序再次运行。为了锻炼 $routeParams,我是否需要在应用程序中添加任何内容?
【解决方案2】:

如果您已经拥有这样的 id,您仍然可以访问它。

  app.controller('customersCtrl', function($scope, $http) {
  var msgId = "20";
  $http.get("message.php?id=" + msgId).then(function (response) {
  $scope.myData = response.data.message;
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2018-07-14
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多