我试图在点击它时更改 href 值。
这是我试过的。
HTML:
<a href="#Page1">Demo</a>
JS:
angular.config(function ($routeProvider) {
$routeProvider
.when('/Page1', {
templateUrl: 'main.html'
controller: 'FirstController'
})
.when('/Page2', {
templateUrl: 'sub.html'
controller: 'FirstController'
})
.otherwise('main.html');
如何在每次单击时更改 anchor 标记的路径。
谁能帮帮我。
最佳答案
为每个页面使用不同的 Controller 。您可以使用链接变量并将其设置在相关 Controller 中。例如:
<a href="#/{{myLink}}">Demo</a>
或
<a ng-href="#/{{myLink}}">Demo</a>
在每个相关的 Controller 中:
$scope.myLink="page1";
或
$scope.myLink="page2";
等等
或者如果你坚持使用相同的 Controller ,你可以检查位置路径:
if ($location.path().substring(1) == "page1") {
$scope.myLink="page2";
}
关于javascript - 动态改变href angular JS的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365840/