ihadjiat ihadjiat - 5 months ago 13
AngularJS Question

How to pass argument from module A to module B?

I'm learning AngularJs and I try to use a one page/one module pattern with one super module on the index.

Right now, I have a route to open a page which display a a specific recipe.
I am able to display my page using routing. I've put a parameter in my route, but I can't access my parameter in my recipe_module.

Here is my code :

my index module

var layout = angular.module( 'LayoutApp', [
// Dépendances du "module"
'ngRoute',
'RecipeDetailsApp'
]);
layout.config( [ '$routeProvider', function( $routeProvider ) {
$routeProvider
.when( '/recettes_details/:idRecette', {
templateUrl: 'partials/recettes_details.html',
} );
}]);


my recipe module :

var RecipeDetails = angular.module( 'RecipeDetailsApp', [ 'ngFactoryApp', 'ngRoute' ] );

RecipeDetails.controller( 'RecipeDetailsCtrl', [ 'ngFactory', function( ngFactory, $routeParams ) {
this.recipeId = $routeParams.param;

}]);


And finally my recipe HTML

<div ng-app="RecipeDetailsApp" ng-controller="RecipeDetailsCtrl as recipeDetailsCtrl">
<h1> {{RecipeDetailsCtrl.recipeId}}</h1>
</div>


How can I provide any parameters to my recipe_module without using a $scope variable?
Thanks

Answer

I've finally understood my mistake. I just had a bad injection syntax... I wrote :

RecipeDetails.controller( 'RecipeDetailsCtrl', [ 'ngFactory', function( ngFactory, $routeParams ) {
this.recipeId = $routeParams.param;

}]);

where I should have written :

RecipeDetails.controller( 'RecipeDetailsCtrl', '$routeParams, [ 'ngFactory', function( ngFactory, $routeParams ) {
this.recipeId = $routeParams.param;

}]);

and now, I can send parameter from a page to another.

Thanks!

Comments