使用AngularJS和模态对话框清理URL

Pat*_*ody 5 modal-dialog twitter-bootstrap angularjs angular-ui-bootstrap

我正在使用AngularJS + UI Bootstrap提供的"模态"指令.模态对话框效果很好,但我有一个问题:URL没有变化.

例如,我的AngularJS应用程序具有为"/ people"设置的路线,该路线显示人员列表.当用户点击某个人时,会出现一个模态对话框并显示详细信息.我试图弄清楚如何修改URL而不实际导致$ routeProvider重新执行控制器.

例如,我可以在我的路线中有类似的东西:

$routeProvider.
when('/people', {controller: PeopleCtrl, templateUrl: 'templates/people.html'}).
when('/people/:personId', {controller: PeopleCtrl, templateUrl: 'templates/people.html'}).
Run Code Online (Sandbox Code Playgroud)

然后在我的PeopleCtrl中我可以这样做:

if ($routeParams.personId) {
    $scope.editPerson({id: $routeParams.personId});
}
Run Code Online (Sandbox Code Playgroud)

这基本上调用了我绑定的同一个函数,点击人行.这适用于允许我的应用程序响应像/ people/123这样的URL,但我无法弄清楚如何让URL从/ people跳转到/ people/123然后回到/ people作为模态对话框打开和关闭.

显然,我可以调用$ location.path("/ people"),但问题是PeopleCtrl得到重新启动,我想避免这种情况,因为应该保留模态"后面"的状态.

换句话说:我正在寻找一种方法来更改URL /位置,而不会检测到正常的$ routeProvider更改.

谢谢!

Pat*_*ody 5

发现另一个SO帖子,它完全符合我的要求:

function MyCtrl($route, $scope) {
    var lastRoute = $route.current;
    $scope.$on('$locationChangeSuccess', function(event) {
        $route.current = lastRoute;
    });
}
Run Code Online (Sandbox Code Playgroud)