【问题标题】:ngTouch module prevent redirect to url from href attributengTouch 模块阻止从 href 属性重定向到 url
【发布时间】:2016-01-06 00:23:50
【问题描述】:

我在移动设备上的应用程序出现问题。我有这样的事情:

<a href="#!/kategori/hva-skjer" ng-click="toggleMenu()">

这是菜单中的链接之一。我想要并且在 dekstop 上运行良好的是关闭菜单并从 href 属性重定向到正确的 url。

这不适用于移动设备(也适用于 chrome devtools 的模拟器)。它正在切换菜单,但没有重定向。

当我从应用程序引导文件中的依赖项中删除 ngTouch 模块时,问题消失了。

有人有类似的情况并且已经知道解决方案吗?还是ngTouch模块的bug?

演示

在您的开发工具中启用触摸仿真并单击链接。如您所见,toggle 函数在链接无效时起作用。

http://plnkr.co/edit/1TldHkHvVfo4OmH7MLk4?p=preview

【问题讨论】:

    标签: javascript angularjs angularjs-ng-touch


    【解决方案1】:

    我不知道这是否仍然有帮助,因为这个问题已经存在一年多了,但我最近遇到了完全相同的问题。似乎有一个关于 ngTouch 的已知错误。 (https://github.com/angular/angular.js/issues/5307)

    对我有用的解决方案如下:

    将您的 ng-touch 文件 (angular-touch.js) 的内容替换为您可以找到的文件 here。 (这一步可能对某些人来说不是必需的,但对我来说是出于某种原因。)

    然后进入文件并更改以下行:

    ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement',
    function($parse, $timeout, $rootElement) {
    

    到:

    ngTouch.directive('ngClick', ['$parse', '$timeout', '$rootElement', '$location',
    function($parse, $timeout, $rootElement, $location) {
    

    然后找到这部分:

        if (!angular.isDefined(attr.disabled) || attr.disabled === false) {
           element.triggerHandler('click', [event]);
         }
    

    然后像这样在它下面添加一个 if :

    if (!angular.isDefined(attr.disabled) || attr.disabled === false) {
           element.triggerHandler('click', [event]);
         }
    if the element has an href attribute, ensure that the url gets updated.
       if (attr.href && angular.isString(attr.href)) {
        $location.url(attr.href);
       }
    

    您也可以停止使用 href 并手动将您的 href 更改为 location.go() 函数,但此解决方案会为您做到这一点。

    【讨论】:

      【解决方案2】:

      这是一个老错误,在此之前没有修复。 Href 和 ng-click 不能一起使用。

      下面您将了解如何避免此错误。

      https://stackoverflow.com/a/34612541/4245233

      【讨论】:

        猜你喜欢
        • 2015-09-02
        • 2016-09-14
        • 2015-06-15
        • 1970-01-01
        • 1970-01-01
        • 2019-08-18
        • 2015-04-21
        • 2015-10-05
        • 1970-01-01
        相关资源
        最近更新 更多