【问题标题】:how to redirect one html page to another using button click event in angularjs如何使用angularjs中的按钮单击事件将一个html页面重定向到另一个页面
【发布时间】:2017-10-26 14:28:47
【问题描述】:

我正在使用 angularjs 指令重定向另一个 html 页面。我在指令中编写 click 函数,但我在警告框中获取页面名称而不是重定向到另一个 html 页面。

示例.html:

<test1>
<button data-ng-click="click('/page.html')">Click</button>
</test1>

sample.js:

app.directive('test1',['$location', function(location) {
    function compile(scope, element, attributes,$location) {
         return{
             post:function(scope, element, iAttrs,$location) {
                 scope.click=function(path)
                 { 
                     alert("click"+path);
                     $location.path('/path');
                 };
             }
         };
        }

        return({
            compile: compile,
            restrict: 'AE',
        });

    }]);

我想重定向 page.html 如何做到这一点请建议我。 谢谢。

【问题讨论】:

    标签: angularjs angularjs-directive


    【解决方案1】:

    在html中,

    <button ng-click="redirect()">Click</button>
    

    在 JS 中,

    $scope.redirect = function(){
      window.location = "#/page.html";
    }
    

    希望对你有帮助.....

    【讨论】:

      【解决方案2】:

      可能会稍作修改。

      注入$location并使用:

      $scope.redirect = function(){
        $location.url('/page.html');
      }
      

      可以在文档中找到使用 $location 而不是 window.location 的好处,但这里是 (1.4.x) 的摘要

      https://docs.angularjs.org/guide/$location

      如果有人知道如何以更简洁的方式直接从 html 进行重定向,请告诉我,因为有时我宁愿这样做而不是在控制器中创建函数...

      【讨论】:

        【解决方案3】:

        我已经在 AngularJS 中使用以下代码成功导航 PAGE。

        $scope.click = function ()
        {
            window.location = "/Home/Index/";
        }
        

        【讨论】:

          【解决方案4】:

          如果您使用材料设计按钮“md-button”,您可以这样做。

          我已经尝试给属性href,分配了链接路径。重定向效果很好。

          如果只是重定向,并且在重定向之前没有其他任务要做,可以尝试。

          <test1>
            <md-button href="/page.html">Click</md-button>
          </test1>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-12-06
            • 1970-01-01
            • 2019-09-15
            • 2021-09-07
            • 1970-01-01
            • 1970-01-01
            • 2013-08-17
            • 2012-12-04
            相关资源
            最近更新 更多