2017-06-15 38 views
1

我有一個用Angular製作的網站,當selectbox中的某個選項被更改時,我想更改url的名稱。就像這樣:用Angular更改url的名稱

本地主機:3000 /管道工-IN /阿姆斯特丹

並在已選擇的城市 「烏得勒支」,該網址已經被更改爲:

本地主機:3000 /管道工,在/烏得勒支

這是我的選擇框:

<div class="plumber-by-city col-sm-12 home-text-col"> 
<div class="title-1">Seach by city</div> 
<select ng-model="selectedItem" ng-change="change()" ng-options="item as item.city for item in items"></select><span class="fa fa-caret-down"></span> 

在我的app.js:

app.config(function($routeProvider, $locationProvider,$animateProvider) { 
    $routeProvider 

    .when('/plumber-in/:type', { 
     templateUrl: 'partials/plumber-by-location.html', 
     controller: 'DataCtrl' 
    }) 
}); 

在我controller.js:

app.controller('DataCtrl', function($scope,$http,$routeParams){ 

    $scope.change = function() { 
    $scope.type = $routeParams.type; 
    console.log($scope.type); 
    $rootScope = $scope.selectedItem.city; 
    console.log($rootScope); 
    }; 
}); 

回答

2

快速和可行的解決方案將是

app.controller('DataCtrl', function($scope,$http,$location){ 
    $scope.change = function() { 
     $location.path('/plumber-in/' + $scope.selectedItem.city); 
    }; 
})