2017-08-09 220 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