简体   繁体   English

如何使用angular.js传递参数?

[英]How to pass params using angular.js?

I am building my first angular app and I have almost finished it but I have one page left to complete that is causing me a bit of a headache. 我正在构建我的第一个有角度的应用程序,我几乎完成了它,但是我只剩下一页要完成,这让我有些头疼。

So I have a page that lists trips I have been on. 因此,我有一个页面列出了我曾经去过的旅行。 This is done with the following code. 这是通过以下代码完成的。

<?php
 $sql = "SELECT * FROM tripreport WHERE userid = $userid LIMIT 10";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
              while($row = $result->fetch_assoc()) {
      ?>
<div class="row">
   <div class="col-lg-7 col-md-7">
        <a href="#">
            <img class="img-responsive" src="reportimages/<?php echo $row['banner_img'];?>" alt="">
        </a>
    </div>
    <div class="col-lg-5 col-md-5">
        <h3><?php echo $row['reportname'];?></h3>
        <h4><?php echo $row['reportstartdate'];?>
            <strong class="pull-right "><?php echo $row['likes'];?> Likes</strong>
        </h4>
        <hr class="border-gray">
        <?php echo $row['reportdescription'];?>
        <hr class="border-gray">
        <a class="btn bg-green" href="#!/report-listing"></span>Manage</a>
        </div>
    </div>
</div>

So my URL is the below: 所以我的网址如下:

<a class="btn bg-green" href="#!/report-listing"></span>Manage</a>

I need this to get the ID from the database and have this posted to the report listing page so I can pull out corresponding reports. 我需要它来从数据库中获取ID,并将其发布到报告列表页面,以便我可以提取相应的报告。

I assume something needs to go in to my angular-menu.js JS to do this which currently only has: 我认为我的angular-menu.js JS需要执行一些操作,目前仅具有:

         when('/report-listing', {
        templateUrl: 'pages/report-listing.php',
        controller: ReportListingsCtrl,
        activetab: 'report-listing'
    }).

Also I think I will need something in my controller.js file which I currently have set as: 我也想在我的controller.js文件中需要一些当前设置为:

function ReportListingsCtrl($scope, $http, $timeout) {}

Any help would be appreciated. 任何帮助,将不胜感激。

Firstly, tell your destination controller (the page you are referring to) to expect and accept a parameter when we navigate to that page.In your app.js, introduce :reportId param to send the id. 首先,告诉目标控制器(您所指的页面)在导航到该页面时期望并接受一个参数。在您的app.js中,引入:reportId参数来发送ID。

$routeProvider.when('/report-listing/:reportId?', {
            templateUrl: 'pages/report-listing.php',
            controller: ReportListingsCtrl,
            activetab: 'report-listing'
        }).

The question mark after the parameter denotes that it's an optional parameter. 参数后面的问号表示它是可选参数。

<a class="btn bg-green" href="#!/report-listing/3"></span>Manage</a>

And in the Controller.js, you access the id as such. 在Controller.js中,您可以这样访问ID。

function ReportListingsCtrl($scope, $http, $timeout,$routeParams) {
$scope.id=$routeParams.reportId;
}

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

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