简体   繁体   中英

Angular: Using $location.path() instead of href

I recently began using $location.path() within ng-click functions rather than simply referencing a path within the href of an a tag. My reasons for doing so:

  • Preference to have ALL logic, including paths navigated to moved out of my views (this may be a bit extreme).
  • For rare instances where multiple html templates reference the same controller I can change $location.path() once rather than having to remember to update corresponding href's within each template.

So instead of:

<a class="button button-balanced" href="/signup">Sign up</a>

I have:

<button class="button button-balanced" ng-click="goToSignup()">Sign up</button>

and in my controller:

$scope.goToSignup = function() {
    $location.path('/signup');
}

My question is, are there drawbacks to setting my Angular app up this way? (note: I'm mainly building Ionic hybrid mobile apps)

  • Preference to have ALL logic, including paths navigated to moved out of my views (this may be a bit extreme).

You can do with your all logic and paths navigated by using same link button

<a href="/signup" class="button button-balanced" ng-click="SignUpFunc()>Sign Up</a>

the a tag is first execute the click function, then execute the href call. So you can do it by link button.

  • For rare instances where multiple html templates reference the same controller I can change $location.path() once rather than having to remember to update corresponding href's within each template.

If you using menu option, you can use link button but if you navigate one controller to another controller, then you should use the $location.path('/signup');

I also used similar logic in my project.It works fine for me.

<button class="button" ng-click="go('view')">Next Page</button>

    $scope.go = function ( path ) {
        $location.path( path );
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM