【问题标题】:dynamically change the path of href angular JS动态改变href angular JS的路径
【发布时间】:2015-05-06 01:11:30
【问题描述】:

我正在尝试更改单击它的 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');

如何在每次点击锚标签时更改其路径。

谁能帮帮我。

【问题讨论】:

  • 您应该使用ng-href 来更改href 属性的值。当您的控制器值更改时,这将自动更改 href。

标签: javascript jquery angularjs angularjs-directive


【解决方案1】:

为每个页面使用不同的控制器。您可以将变量用于链接并在相关控制器中进行设置。例如:

<a href="#/{{myLink}}">Demo</a>

<a ng-href="#/{{myLink}}">Demo</a>

并在每个相关的控制器中:

$scope.myLink="page1";

$scope.myLink="page2";

等等

或者如果你坚持使用同一个控制器,你可以查看位置路径:

if ($location.path().substring(1) == "page1") {
    $scope.myLink="page2";
}

【讨论】:

  • 类似,但 ng-href 是更好的选择!
  • 如果不解释或链接到解释为什么它是一个更好的选择,那就没用了...... - 没关系,只是在上面的评论和下面的其他解决方案中找到它。
【解决方案2】:

改用ng-href - 它会在绑定值更改时自动更新。

【讨论】:

    猜你喜欢
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2020-04-23
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多