user4789180
user4789180

Reputation:

How to get ui-router state params?

I am trying to create a food planner that allows the user to input their meals. They access it through fixed monday-sunday buttons, and the url looks like:

#/planner/Monday/add-meal

I am trying to create new objects to an array that takes:

  $scope.meal = {
    day: [THE DAY IN THE URL],
    title: '',
    description:''
  };

Do I need to use $root service?

I'm using the Ionic framework. Any help would be greatly appreciated.

Upvotes: 0

Views: 59

Answers (1)

sp00m
sp00m

Reputation: 48807

In your router, use the following URL for the state:

/planner/:dayOfWeek/add-meal

Then, in your controller, inject $stateParams:

.controller("TheController", ["$scope", "$stateParams", function ($scope, $stateParams) {

  $scope.meal = {
    day: $stateParams.dayOfWeek,
    title: '',
    description:''
  };

}]);

See https://github.com/angular-ui/ui-router/wiki/URL-Routing#url-parameters.

Upvotes: 1

Related Questions