在Angularjs的UI路由器中,如何将包含10s参数的对象$rootscope传递到状态?

在Angularjs的UI路由器中,如何将包含10s参数的对象$rootscope传递到状态?,angularjs,angular-ui-router,ui-sref,Angularjs,Angular Ui Router,Ui Sref,您好,我是一名新开发人员,我在使用UI路由器将根作用域参数传递到我的状态时遇到此问题 这是我的href,我想将其更改为ui sref: href="/mycarts/{{cats.id}}?{{$root.filterParams}} 以下是我的状态: .state('mycarts', { url : '/mycarts/:id?vendors&' + 'price_to&price_from&manufacturer&' +

您好,我是一名新开发人员,我在使用UI路由器将根作用域参数传递到我的状态时遇到此问题

这是我的href,我想将其更改为ui sref:

href="/mycarts/{{cats.id}}?{{$root.filterParams}}
以下是我的状态:

.state('mycarts', {
    url : '/mycarts/:id?vendors&' +
          'price_to&price_from&manufacturer&' +
          'editor&theme&page&order&filter_by&no',
    templateUrl : 'partials/mycarts.html',
    controller : 'MyCartsController'
这就是我将参数传递给控制器的方式:

    $scope.id = $stateParams.id;

因此,基本上id参数的所有功能都很好,但我也需要传递其他字符串查询,这非常多,我不知道如何传递其他字符串查询。将上面的href更改为ui sref的正确方法是什么。

我会创建一个对象,以便在控制器中的
ui sref
中使用,就像这样

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

然后在
ui sref

ui-sref="mycart(linkParams)"

如果确实不需要使用href,请在控制器中编写一个函数,并在单击按钮或链接时调用它

HTML

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

ui-sref=“mycarts({id:cats.id,vendors:$root.filterParams.vendors})
@Phil:事实上,$root.filterParams包含对象中的所有查询字符串值。事实上,还应该传递所有字符串参数??比如:
ui-sref=“mycarts({id:cats.id,vendors:$root.filterParams.vendors,price_to:$root.filterParams.price_to,price_from:$root.filterParams.price_from,其余})
..这是干净的方式吗?你可以,但只有在状态配置中定义的参数可以通过
$stateParams
@Phil获得是的…是的,我也不想让它在StackOverflow中可读。这个问题的整个前提是令人困惑的。使用
$rootScope
的关键是存储参数在变量之后创建的所有
$scope
对象都可以看到,这意味着您根本不需要在应用程序中传递
$rootScope
参数。但是,以这种方式使用
$rootScope
是一种容易出错的反模式,建议使用ser来存储多个控制器的属性恶习
.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)
})