Search code examples
angularjsangular-ui-routerui-sref

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.


Solution

  • 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)"