2014-10-31 86 views
-1

我在操作ng路由功能主义者方面存在重大差距。 这是我想要实现的:我有一个主页面,指向dashboard.html和另一个 buildings.html。 ID,类型,颜色 因此,一个传统的URL会看起来像:ngroute支持多个url参数

/buildings?id=110&type=special&color=red 

所以据我了解的角度ngroute我应该有这种结构 建筑物页可以用许多参数,如被称为:

$routeProvider 

      // route for the main page which will direct to the buildings page 
      .when('/', { 
       templateUrl : 'web/pages/dashboard.html', 
       controller : 'dashboardController', 
       controllerAs : 'dashboard' 
      }) 

      // route for the main page which will direct to the buildings page 
      .when('/buildings/:buildingId/:buildingType/:buildingColor', { 
       templateUrl : 'web/pages/buildings.html', 
       controller : 'mainController', 
       controllerAs : 'buildings' 
      }) 
      ; 

}); 

和URL应该是这样的:

/buildings/110/special/red 

我不明白的是如何调用对于只有IDØ建筑物页与类型和颜色? 如果我有7个参数,并且只想触发3个呼叫,我该怎么做?

我使用$ location.path在需要时根据gui事件在页面之间切换,例如: $ location.path('/buildings/'+$scope.id);

谢谢。

回答

0

你这里有三个选项:

  • 要么你会orther你的参数,这样,当你想使用PARAM 5,你是肯定的,那以前的PARAMS也必须定义
  • 或者你可以使用一些代表null的字符,比如 - 或者其他。你的URL看起来像/buildings/5/-/red
  • 或(最好的选择),你应该离开强制性参数中的URL(如IDS)和放在URL可选参数得到的参数(如你在旧校园风格的参数显示)。那么你的网址将如下所示:/buildings/5?color=red。您可以使用$location.search().color等从AngularJS访问这些参数。
+0

我尝试了第三种方法,但无法使用location.path生成url字符串,例如$ location.path('/ buildings /'+ $ scope.multiElement.id)生成:/ building%3FId = 110。看起来像?编码为 – 2014-10-31 16:43:25

+0

这里有两种方法: a)'$ location.url('/ buildings/5?color = red&type = special');' b)'$ location.search('color','red' ).search('type','special')。path('/ buildings/5');' – 2014-10-31 16:47:18

+0

是的,但是如果我打算采用第三种方法,我怎样才能使用角度生成传统的url?也有人说这不被推荐,因为它会使页面重新加载 – 2014-10-31 16:56:00