AngularJS 路由和位置

AngularJS routing and location

是否有使用 routing/location 信息构建 url 的直接方法。

我想在模型更改时自动重定向。 我正在考虑创建一个模块,允许您指定要观看的对象。对这些对象的更改会触发使用 $location 的重定向,但我不知道是否可以从路由中获取路由映射。

Angular 有一个 'official' 模块可以做这样的事情叫做 ngRoute - although many users prefer the use of uiRouter(包括我自己)。这样做的方法是在控制器中使用 $scope.$watch,或 ng-change 触发 $state.go(对于 uiRouter)或 $location.url(对于 ngRoute)。

这是 uiRouter 中的一个潜在示例:

<div ng-controller='FooCtrl as foo'>
  <input type='text' ng-model='foo.bar' ng-change='foo.update(foo.bar)'>
</div>
angular.controller('FooCtrl', function($state) {
  this.update = function (model) {
    // depending on some properties in the model..
    $state.go('mystate', { model: model });
  };
})