Skip to content

Commit 9ed1126

Browse files
committed
docs($location): update replace() docs
1 parent 7a19eb8 commit 9ed1126

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

docs/content/guide/dev_guide.services.$location.ngdoc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,25 @@ All of the setter methods return the same `$location` object to allow chaining.
126126
change multiple segments in one go, chain setters like this:
127127
<pre>$location.path('/newValue').search({key: value});</pre>
128128

129-
All setter methods take an optional boolean flag parameter, which signifies whether current history
130-
record should be replaced or if a new record should be created (default). To change the current URL
131-
without creating a new browser history record you can call:
132-
<pre>$location.path('/newVal', true);</pre>
129+
There is a special `replace` method which can be used to tell the $location service that the next
130+
time the $location service is synced with the browser, the last history record should be replaced
131+
instead of creating a new one. This is useful when you want to implement redirection, which would
132+
otherwise break the back button (navigating back would retrigger the redirection). To change the
133+
current URL without creating a new browser history record you can call:
134+
<pre>
135+
$location.path('/someNewPath');
136+
$location.replace();
137+
// or you can chain these as: $location.path('/someNewPath').replace();
138+
</pre>
133139

134140
Note that the setters don't update `window.location` immediately. Instead, `$location` service is
135141
aware of the {@link api/angular.scope scope} life-cycle and coalesces multiple `$location`
136-
mutations into one "commit" to the `window.location` object during the scope `$flush` phase. Since
137-
any of the setters can take the replace flag, it's enough for one setter to use this flag in order
138-
to make the entire "commit" a replace operation rather than addition to the browser history.
142+
mutations into one "commit" to the `window.location` object during the scope `$digest` phase. Since
143+
multiple changes to the $location's state will be pushed to the browser as a single change, it's
144+
enough to call the `replace()` method just once to make the entire "commit" a replace operation
145+
rather than addition to the browser history. Once the browser is updated, the $location service
146+
resets the flag set by `replace()` method and future mutations will create new history records,
147+
unless `replace()` is called again.
139148

140149
### Setters and character encoding
141150
You can pass special characters to `$location` service and it will encode them according to rules

0 commit comments

Comments
 (0)