Suppose you are using routes:
// bootstrap
myApp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeCtrl'
});
$routeProvider.when('/about', {
templateUrl: 'partials/about.html',
controller: 'AboutCtrl'
});
...
And in your html, you want to navigate to the about page when a button is clicked. One way would be
<a href="#/about">
... but it seems ng-click would be useful here too.
- Is that assumption correct? That ng-click be used instead of anchor?
- If so, how would that work? IE:
<div ng-click="/about">
Another solution but without using ng-click which still works even for other tags than
<a>
:This way you can also pass parameters to your route: https://stackoverflow.com/a/40045556/838494
(This is my first day with angular. Gentle feedback is welcome)
You can use:
If you want some dynamic variable inside href you can do like this way:
Where link is Angular scope variable.
Routes monitor the
$location
service and respond to changes in URL (typically through the hash). To "activate" a route, you simply change the URL. The easiest way to do that is with anchor tags.Nothing more complicated is needed. If, however, you must do this from code, the proper way is by using the
$location
service:Which, for example, a button could trigger:
just do it as follows in your html write:
And in your controller, add $state as follows:
Remember that if you use ng-click for routing you will not be able to right-click the element and choose 'open in new tab' or ctrl clicking the link. I try to use ng-href when in comes to navigation. ng-click is better to use on buttons for operations or visual effects like collapse. But About I would not recommend. If you change the route you might need to change in a lot of placed in the application. Have a method returning the link. ex: About. This method you place in a utility
I used
ng-click
directive to call a function, while requesting route templateUrl, to decide which<div>
has to beshow
orhide
inside route templateUrl page or for different scenarios.AngularJS 1.6.9
Lets see an example, when in routing page, I need either the add
<div>
or the edit<div>
, which I control using the parent controller models$scope.addProduct
and$scope.editProduct
boolean.RoutingTesting.html
TestingPage.html
both pages -
RoutingTesting.html
(parent),TestingPage.html
(routing page) are in the same directory,Hope this will help someone.