Angular/UI-Router - 如何在不刷新所有内容的情况下更新URL?

ac3*_*360 15 javascript angularjs angular-ui-router

我是Angular和ui-router的新手.当有人选择模型并且我的控制器执行标准的SHOW方法时,我只想更新URL以包含模型的ID而不刷新页面,并继续我的原始视图.我不知道怎么做...

$stateProvider
        .state('citylist', {
          url: "/",
          templateUrl: "views/index.html"
        })
        .state('cityshow', {
          url: "/city/:id",
          templateUrl: "views/index.html"
        })

angular.module('app.system').controller('IndexController', ['$scope', 'Global', 'Cities', '$stateParams', '$location', function ($scope, Cities, $stateParams, $location) {

    $scope.loadTown = function(id) {
        Cities.get({id: id }, function(city) {
             $scope.city = city
             // Change URL
             // Do things within the same view...
        });
    };
});

<button ng-click="loadTown(123456)">Chicago</button>
Run Code Online (Sandbox Code Playgroud)

Rad*_*ler 6

您使用的代码段与正确的ui-router设置相差甚远.我建议你查看这个演示应用程序:http://angular-ui.github.io/ui-router/sample/#/contacts/1.在这里,我们可以看到如何在没有任何页面刷新的情况下更改细节时如何"修复" 列表.

这个例子的代码在这里下载https://github.com/angular-ui/ui-router/tree/master/sample.

什么应该有很多帮助理解如何(除了维基)是这段代码片段:state.js.加载演示,阅读此注释说明.并且ui-router会有意义......

  • 我相信你已经看到了这个:http://egghead.io/lessons/angularjs-introduction-ui-router Radim的建议很好.大多数jsfiddles,plunkers和文章已经过时了,虽然ui-router并没有什么特别之处,但除非找到正确的来源,否则需要一段时间才能实现. (2认同)