Farbod Ghiasi
Farbod Ghiasi

Reputation: 55

how to pass $rootscope which is an object includeing 10s of parameter to state in UI-Router of Angularjs?

Hello I am a new developer, I have this problem passing a root-scope parameter to my state using UI-Router.

Here is my href which i want to change it to ui-sref :

href="/mycarts/{{cats.id}}?{{$root.filterParams}}

Here is my state:

.state('mycarts', {
    url : '/mycarts/:id?vendors&' +
          'price_to&price_from&manufacturer&' +
          'editor&theme&page&order&filter_by&no',
    templateUrl : 'partials/mycarts.html',
    controller : 'MyCartsController'

And this is how i passed parameter to controller:

    $scope.id = $stateParams.id;

so basically everything for id parameter works fine but i need to pass other string query as well which are a lot , I don't know how to pass other string query to it what is the correct way of changing my href above to ui-sref.

Upvotes: 2

Views: 319

Answers (2)

Mathews
Mathews

Reputation: 909

If it is not really required to use href, write a function in the controller and invoke it as onclick of your button or link.

HTML

<a style="text-decoration: underline" href="" ng-click="gotToMycarts()">Go</a>

Javascript

.controller('TestController',function($state){
   $state.transitionTo('mycarts',{id :12,vendors:'Test',price_to:200});
}).controller('MyCartsController',function($state, $stateParams){
  console.log($stateParams.id);
  console.log($stateParams.price_to)
})

Upvotes: 0

Phil
Phil

Reputation: 164760

I'd create an object for use in ui-sref in your controller like so

$scope.linkParams = angular.extend({id: $scope.cats.id}, $scope.$root.filterParams);

(see https://docs.angularjs.org/api/ng/function/angular.extend)

Then use it in ui-sref

ui-sref="mycart(linkParams)"

Upvotes: 2

Related Questions