2014-10-17 63 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