如何使用angularjs中的按钮单击事件将一个html页面重定向到另一个html页面

use*_*122 5 angularjs angularjs-directive

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

sample.html:

<test1>
<button data-ng-click="click('/page.html')">Click</button>
</test1>
Run Code Online (Sandbox Code Playgroud)

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',
        });

    }]);
Run Code Online (Sandbox Code Playgroud)

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

小智 22

在HTML中,

<button ng-click="redirect()">Click</button>
Run Code Online (Sandbox Code Playgroud)

在JS中,

$scope.redirect = function(){
  window.location = "#/page.html";
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.....


Thi*_*obo 8

可能会做一个小修改.

注入$ location并使用:

$scope.redirect = function(){
  $location.url('/page.html');
}
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

如果有人知道如何直接从html以更干净的方式进行此重定向,请告诉我,因为我更喜欢有时在控制器中创建一个函数...