javascript 如何使用angularjs或coffeescript重定向到ng-click上的另一个页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34263743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:28:10  来源:igfitidea点击:

How to redirect to another page on ng-click using angularjs or coffeescript?

javascripthtmlangularjscoffeescript

提问by Dhana

I am doing some tasks now in angularjs and coffeescript. I have a requirement like: I have couple of buttons in my html page like:

我现在正在用 angularjs 和 coffeescript 做一些任务。我有一个要求,例如:我的 html 页面中有几个按钮,例如:

<div>
    <button type="button" class="btn btn-default" ng-click="button1();">Button1</button>
    <button type="button" class="btn btn-default" ng-click="button2();">Button2</button>
</div>

If I click on Button1, then it should redirect to another page(like: '/test1.html'), similarly, if I click on Button2, then it should redirect to another page(like: '/test2.html'). How can I do this in AngularJS/CoffeeScript ?

如果我点击 Button1,那么它应该重定向到另一个页面(比如:'/test1.html'),同样,如果我点击 Button2,那么它应该重定向到另一个页面(比如:'/test2.html')。我怎样才能在 AngularJS/CoffeeScript 中做到这一点?

If I do in my coffeescript file, I am getting the below error:

如果我在我的 coffeescript 文件中这样做,我会收到以下错误:

app = angular.module('myApp', dependencies)
app.controller 'WizardController', [
  '$scope',
  ($scope) ->
  $scope.button1 = ->
    window.location = '/test1.html'
    return

    $scope.button2 = ->
    window.location = '/test2.html'
    return
  ]

but it is giving compilation error at return statement: Compilation error: Parse error on line 103: Unexpected 'TERMINATOR'

但它在返回语句中给出编译错误:编译错误:第 103 行解析错误:意外的“终止符”

回答by Jax

You can redirect straight from your HTML page:

您可以直接从 HTML 页面重定向:

<div>
  <button type="button" class="btn btn-default" ng-href="/test1.html">Button1</button>
  <button type="button" class="btn btn-default" ng-href="/test1.html">Button2</button>
</div>

Or else in your code you should remove the ';' from the

或者在你的代码中你应该删除';' 来自

ng-click

ng-click

as in:

如:

<div>
  <button type="button" class="btn btn-default" ng-click="button1()">Button1</button>
  <button type="button" class="btn btn-default" ng-click="button2()">Button2</button>
</div>

I will also check the indentation in your coffeescript as it tend to be a bit fuzzy.

我还将检查您的咖啡脚本中的缩进,因为它往往有点模糊。

app = angular.module('myApp', dependencies)
app.controller 'WizardController', [ '$scope', '$location',
($scope, $location) ->
  $scope.button1 = ->
    $location.path('/test1.html')

  $scope.button2 = ->
    $location.path('/test2.html')
]