AngularJS后退按钮 [英] AngularJS back button

查看:158
本文介绍了AngularJS后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作使用的PhoneGap和AngularJS Android应用程序。我试图创建一个按钮来直接用作一个取消和后退按钮,将基本上只是点击浏览器上的后退按钮。

I'm working on making an Android app using Phonegap and AngularJS. I'm attempting to create a button to be used as both a "cancel" and a "back" button, that will essentially just hit the browser's 'back' button.

下面是取消按钮的一些示例HTML:

Here's some sample HTML for the cancel button:

<a href="#" ng-click="goBack()" class="button--cancel weight--bold vertical-align--middle text-center">cancel</a>

这是该页面的控制器,与GoBack的()按钮:

And here is the controller for that page, with the goBack() button:

function NewOccasionCtrl($scope, $window) {
    $scope.$window = $window;
    $scope.goBack = function() {
      $window.history.back();
    };
}

这不引发错误,也没有工作...模拟器保持在同一页上。如果没有 $范围。$窗口= $窗口它抛出一个错误。我希望能实现一个功能返回按钮,而无需创建/使用指令,因为据我了解这些然后实现模板和东西我并不需要/想。

This throws no errors, but also doesn't work... the emulator remains on the same page. Without the $scope.$window = $window it throws an error. I was hoping to achieve a functional 'back' button without having to create/use a directive, because as far as I understand those then implement templating and things I don't need/want.

有没有办法做到这一点?谢谢

Is there a way to do this? Thanks

推荐答案

我去使用指令,使后面的功能重用。这是我最后的code:

I went with using a Directive to make the back functionality reusable. Here is my final code:

HTML

<a href back-button>back</a>

使用Javascript:

Javascript:

app.directive('backButton', function(){
    return {
      restrict: 'A',

      link: function(scope, element, attrs) {
        element.bind('click', goBack);

        function goBack() {
          history.back();
          scope.$apply();
        }
      }
    }
});

这篇关于AngularJS后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆