简体   繁体   中英

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.

I assume something needs to go in to my angular-menu.js JS to do this which currently only has:

         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:

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.

$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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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