Saturday, August 18, 2018

Difference between ui-sref and $state.go in AngularJS UI-Router



Is there any functional difference between ui-sref and $state.go()?



ui-sref is used in ... and $state.go('someState') is used in a controller.



In HTML, I would use:



Link



whereas in a function I would use something like:



if(someCondition) {
$state.go('currentState.state1');
}


So, is that it or do I need to add something after $state.go()? Assuming current state is currentState.


Answer




There is no functional difference between ui-sref and $state.go. See the doc



Activating a state




There are three main ways to activate a state:




  • Call $state.go(). High-level convenience method.

  • Click a link containing the ui-sref directive.


  • Navigate to the url associated with the state.




So, these are at the end doing the same, and as we can see in the code of the ui-sref directive:



...
element.bind("click", function(e) {
var button = e.which || e.button;
if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) {


var transition = $timeout(function() {
// HERE we call $state.go inside of ui-sref
$state.go(ref.state, params, options);
});


it does call $state.go()



And also as discussed here: ui-sref:





ui-sref='stateName' - Navigate to state, no params. 'stateName' can be any valid absolute or relative state, following the same syntax rules as $state.go()



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...