Reputation: 14155
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.
Upvotes: 0
Views: 64
Reputation: 34
Inject $routeParams
into your controller.
myApp.controller("productsController", ['$routeParams', function ($routeParams) {
console.log($routeParams.id);
}]);
Upvotes: 4