Angular.js : How to use ng-href in template?

后端 未结 1 1156
我寻月下人不归
我寻月下人不归 2021-01-31 09:04

I try to create a simple SPA with 1 index.html which include templates.

I got a problem with ng-href directive:

 myPage<         


        
相关标签:
1条回答
  • 2021-01-31 09:48

    ngHref is used to dynamically bind angular variables to the href attribute like so:

    <a ng-href="#/{{myPathVariable}}"/>Take Me Somewhere</a>
    

    Where in your scope:

    $scope.myPathVariable = 'path/to/somewhere';
    

    Then after angular compiles it, it looks like this:

    <a ng-href="#/path/to/somewhere" href="#/path/to/somewhere" ... other angular attributes>Take Me Somewhere</a>
    

    If your path is hardcoded into the page (you know where the link should take you on page load), you can just specify it in an href, which is why your third example works. Only use ngHref when you need angular to decide the route dynamically after the JS loads. This prevents your user from clicking links and going to an invalid route before angular has deciphered where the link should point. Documentation here

    0 讨论(0)
提交回复
热议问题