如何在Angularjs的UI-Router中传递包含10s参数的$rootscope ?

时间:2022-05-25 12:04:02

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

你好,我是一个新开发人员,我有一个问题,就是使用UI-Router将根范围参数传递给我的状态。

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

这是我的href,我想把它改成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.

id参数的所有东西都可以,但我也需要传递其他字符串查询,这很复杂,我不知道如何传递其他字符串查询给它把上面的href改为ui-sref的正确方法是什么。

2 个解决方案

#1


2  

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

我将创建一个对象用于ui-sref中,就像这样

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

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

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

Then use it in ui-sref

然后在ui-sref中使用它。

ui-sref="mycart(linkParams)"

#2


0  

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.

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

HTML

HTML

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

Javascript

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)
})

#1


2  

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

我将创建一个对象用于ui-sref中,就像这样

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

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

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

Then use it in ui-sref

然后在ui-sref中使用它。

ui-sref="mycart(linkParams)"

#2


0  

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.

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

HTML

HTML

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

Javascript

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)
})