Adam Zerner Adam Zerner - 1 year ago 162
AngularJS Question

Can you use ng-click with ui-sref?

Can I do this?

<button ng-click='' ui-sref='state'>test</button>

Answer Source

Yes. ngClick will run, and then you'll transition to your state.


However, if you have an $event.preventDefault() in ngClick, it won't transition you to your state.

Full code example

<!DOCTYPE html>
<html ng-app='app'>

    <script data-require="angular.js@1.4.3" data-semver="1.4.3" src=""></script>
    <script data-require="ui-router@*" data-semver="0.2.15" src="//"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>

  <body ng-controller='MainController as vm'>
      <li><a ui-sref='home'>home</a></li>
      <li><a ui-sref='one' ng-click='vm.test()'>one</a></li>
      <li><a ui-sref='two'>two</a></li>
    <div ui-view></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download