2017-08-09 218 views
2

我想根據國家顯示獨特的頁面。我指的是耐克網站。 https://www.nike.com/sg/en_gb/。在這裏,用戶可以選擇一個地區/國家:基於國家的路由

如何使用路由參數而不是路徑來實現。我想實現使用路由參數或更好的解決方案(如果有的話)。

查看

<body ng-controller="CountryController"> 
    <form> 
     <select name="naver" id="naver" ng-model="route.selectedRoute" ng-change="RedirecttoCountry(route.selectedRoute)"> 
     <option value="/" >Home</option> 
     <option value="/US">US</option> 
     <option value="/UK">UK</option> 
     <option value="/India">India</option> 
     </select> 
    </form> 
    <div ng-view=""></div> 

我對每一個國家單獨HTMLS。

控制器

var app = angular.module("myApp", ['ngRoute']); 

app.config(function($routeProvider){ 
    $routeProvider 
     .when('/', { 
      templateUrl: "US.html", 
      controller: 'CountryController' 
     }) 
     .when('/UK', { 
      templateUrl: "UK.html", 
      controller: 'CountryController' 
     }) 
     .when('/India', { 
      templateUrl: "India.html", 
      controller: 'CountryController' 
     }) 
     .otherwise({ redirectTo: '/' }); 
}); 

app.controller('CountryController', function($scope, $location){ 
    $scope.RedirecttoCountry = function(path){ 
     $location.path(path); 
    } 
}); 
+0

如果我是正確的,你可以通過執行'/添加PARAM:country' –

回答

2

我希望你需要一個路由動態處理所有國家,你可以使用templateUrl如函數定義爲參數:country

var app = angular.module("myApp", ["ngRoute"]); 
app.config(function($routeProvider) { 
    $routeProvider.when('/', { 
     templateUrl: "main.html", 
     controller: 'CountryController' 
    }).when('/:country', { 
     templateUrl: function(params) { 
      return (params.country) + '.html'; 
     }, 
     controller: 'CountryController' 
    }).otherwise({ 
     redirectTo: '/' 
    }); 
}); 
app.controller('CountryController', function($scope, $location) { 
    $scope.RedirecttoCountry = function(path) { 
     $location.path(path); 
    } 
}); 

使用PARAMS工作Plunker。

.when('/:country', { 
    templateUrl: function(params){ 
     return (params.country || 'US')+'.html'; 
    }, 
    controller: 'CountryController' 
}) 
1

你的代碼似乎是工作的罰款。只要確保已經包含了所有的依賴關係並正確關閉了所有的HTML標籤。 https://plnkr.co/edit/hFLCyGgo91GwQhpagvAz?p=preview

+0

鏈接未更新,我相信,我沒有看到鏈接 –

+0

該鏈接僅更新。檢查script.js文件 – Vivz

1

templateUrl可以是參數的功能,例:

$routeProvider 
    .when('/:country', { 
    templateUrl: function(params) { 
     return params.country + '.html'; 
    } 
    }) 
    .when('/', { 
    template: 'default' 
    }) 
    .otherwise({ redirectTo: '/' }); 

參見: https://plnkr.co/edit/rNQutPGZ5b4B0NK5g4Wz?p=preview