2014-10-17 116 views
0

主要问题:是否可以使用$state.go()以及由$location.search(e,v)填充的自定义搜索元素?或者,也许我需要使用$location.path()每次我想进行这样的转换?为什么我需要它:我有一个视图,有几个选择(它们都有一个默认的空值),如果这些选择的每一个改变都会改变位置(fe列表合作伙伴/partners/list?branch=branch01&role=seller)。但通过使用$state.go()我只能使用$stateParams(它是在状态url内部定义的) - 但这并不满足我,我想有一个操作,直接将状态更改为f.e. /partners/list?role=seller带搜索元素的角度ui路由器状态

我想使用$location.search(e,v)的解决方案,因为所有参数都是可选的,所以我无法准备一个可以接受任何搜索参数集的状态。

回答

1

可以使用.go()

// Here's a skeleton app. Fork this plunk, or create your own from scratch. 
 
var app = angular.module('demonstrateissue', ['ui.router']); 
 

 
app.config(function($stateProvider) { 
 
    $stateProvider.state({ 
 
    name: 'home', 
 
    url: '/home?param1&param2', 
 
    controller: function($scope, $stateParams, $state) { 
 
     $scope.$stateParams = $stateParams; 
 
    }, 
 
    template: 'state params: <pre>{{ $stateParams | json }}</pre>'}); 
 
}); 
 

 

 
// Adds state change hooks; logs to console. 
 
app.run(function($state, $rootScope, $location) { 
 
     $rootScope.someparams = function() { $state.go('home', { param1: 'foo', param2: 'bar' }); }; 
 
     $rootScope.otherparams = function() { $state.go('home', { param1: 'wat', param2: 'oy!' }); }; 
 
     $rootScope.location = $location.url; 
 
     $rootScope.$on('$locationChangeSuccess', function(evt, extra) { 
 
$rootScope.location = extra; 
 
     }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
     <script src="https://rawgit.com/angular-ui/ui-router/0.2.11/release/angular-ui-router.js"></script> 
 
    <body ng-app="demonstrateissue"> 
 
     <a href="#" ng-click="someparams()">Some params</a> 
 
     <a href="#" ng-click="otherparams()">Other params</a> 
 
     <div ui-view> 
 
     ui-view not populated 
 
     </div> 
 
     <pre>{{ location }}</pre> 
 
    </body>

+0

的作品就像一个魅力,谢谢应用搜索元素! – patryk 2014-10-18 07:53:45

相关问题