简体   繁体   中英

passing route parameter to controller

Right now I have existing productsController which delivers products data using dataService

myApp.controller("productsController", function ($scope, $http, dataService) {
    $scope.productsData = dataService;    

    dataService.getProducts()
    .then(function () {
        //success

    },
    function () {
        // error
        alert("could not load products");
    });
});

this works fine, products are rendered on my view properly. Now I want to open product details on product click so I add

<tr ng-repeat="product in productsData.products">
    <td>{{ product.Name }}</td>           
    <td>
        <a ng-href="{{ product.Id }}">
            <img ng-src="{{ product.thumbnail }}" width="50" height="50" />
        </a>
    </td>
</tr>  

I added corresponding route

myApp.config(function ($routeProvider) {
    ...
    .when("/product/:id", {
        controller: "productsController",
        templateUrl: "/templates/productDetailsView.html"
    })
    ...
}

my question is: How can I pass this id parameter to productsController so I can pass it further to my data service which will return data.

Inject $routeParams into your controller.

myApp.controller("productsController", ['$routeParams', function ($routeParams) {
    console.log($routeParams.id);         
}]);

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