asp.net - 如何在 Angular 路由中附加 URL?

标签 asp.net angularjs asp.net-mvc-5 angularjs-ng-repeat angular-ui-router

如何在 Angular 路由中进行 url(string) 串联。

请引用下面的代码片段来理解我的问题。

app.config(['$routeProvider', '$location'], function($routeProvider) {
  when('/', {
    templateUrl: '/AlbumsList.html',
    controller: 'a1Ctrl'
  }).
  when('/albums/:albumName', {
    templateUrl: 'AlbumsList.html',
    controller: 'b1Ctrl'
  })
})
app.controller('a1Ctrl', function($scope, $http) {

  $scope.albums = function() {
    //ajax getting data from server 
  }
})

app.controller('b1Ctrl', function($scope, $routeProviders) {
  $scope.album = $routeProviders.albumName;

  $http({
    //same ajax function as above getting data from server for the nes sub album
  })
})
<div>
  <ul>
    <li ng-repeat="album in albums">
      <a href="/albums/{{album.name}}">{{album.name}}</a>
    </li>
  </ul>

</div>

现在页面加载时,执行第一个路由路径,页面显示如下 Output

现在,当我点击浏览器中的印地语专辑 url 时,它会更改为/albums/hindi 并且 html 呈现如下

enter image description here

当前浏览器中的 url 已更新为/localhost:8080/albums/himesh 现在我的需要是将浏览器中的网址更新为 /localhost:8080/albums/hindi/himesh

那么如何使用 Angular 路由进行此类字符串连接。

谢谢你。

最佳答案

您需要更改您的 $routeProvider.when url 选项,该选项将接受两个参数,一个是 albumType ,另一个是 alubmName 。您可以使用 $routeParams 服务在 b1Ctrl 中获取这些值。

标记

<li ng-repeat="album in albums">
  <a ng-href="/albums/{{alubm.type}}/{{album.name}}">{{album.name}}</a>
</li>

代码

when('/albums/:albumType/:albumName', {
    templateUrl: 'AlbumsList.html',
    controller: 'b1Ctrl'
})

然后在 Controller 内,您应该使用 $routeParams 而不是 $routeProviders

Controller

app.controller('b1Ctrl', function($scope, $routeParams) {
  $scope.album = $routeParams.albumName;
  $scope.albumType = $routeParams.albumType; //alumType eg. hindi , english, etc

  $http({
    //same ajax function as above getting data from server for the nes sub album
  })
});

关于asp.net - 如何在 Angular 路由中附加 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875750/

相关文章:

c# - 改进 GUI/网站的设计

c# - 如何在C#中使用imageurl显示图像

asp.net - 将异常信息从 Global.asax 传递到 ErrorPage.aspx

javascript - 订单函数调用

html - 在 AngularJS 中使用 ng-print 时如何应用 css 样式

asp.net - 页面刷新再次触发事件

javascript - 两种方式的数据绑定(bind)在 AngularJS 中不起作用

asp.net-mvc - 我的问题是 "Server cannot set status after HTTP headers have been sent."

authentication - 使用 ExternalLoginCallback 进行身份验证如何获取 UserId 和 UserName ASP.NET MVC5

c# - 在azure中部署MVC5应用程序