【问题标题】:Uncaught Error: Syntax error, unrecognized expression: #/ angularjs and jquery未捕获的错误:语法错误,无法识别的表达式:#/ angularjs 和 jquery
【发布时间】:2015-10-08 12:04:25
【问题描述】:

每当我的页面加载时,我的控制台中都会显示以下错误。

“未捕获的错误:语法错误,无法识别的表达式:#/about”

我的代码如下:

                <li class="active">
                    <a href="/">Home</a>
                </li>
                <li class="">
                    <a href="#/about" title="About Us">About</a>
                </li>
                <li class="">
                    <a href="#/pricing">Pricing</a>
                </li>

它显示 .最初,它首先显示 #/about,如果我删除 about us 标签,它将显示定价页面链接。

不是引导标签问题,这只是简单的导航

app.js 中的路由代码:

$routeProvider.when('/', {
        templateUrl: 'partials/home.html'
    }).when('/account', {
        templateUrl: 'partials/account.html',
    }).when('/terms', {
        templateUrl: 'partials/terms.html'
    }).when('/about', {
        templateUrl: 'partials/about.html'
    }).otherwise({
        redirectTo: '/'
    });

我已经调试了它的引导问题,引导导航导致了这个问题:“nav navbar-nav” 对此的修复是 data-target="#" 但希望对此进行适当的解决

【问题讨论】:

  • 你的路由相关的JS代码是什么?
  • 使用&lt;a ng-href="/about" title="About Us"&gt;About&lt;/a&gt;
  • 试过还是同样的错误
  • 我已经调试过了,是因为bootstrap,bootstrap navbar="nav navbar-nav"

标签: jquery angularjs twitter-bootstrap


【解决方案1】:

这很可能是 Bootstrap 问题。尝试在这样的链接上使用data-targetattribute

<a href="#/about" data-target="#about" title="About Us">About</a>

【讨论】:

  • 它与模态或引导组件无关,它只是一个导航链接
  • 这通过添加 data-target="#" 解决了我的问题
  • 所有路线都可以除了“/”a(href='#/', data-target='#')。我的路由配置是一样的:$routeProvider.when('/', {templateUrl: /views/home/home.html'})。我做错了什么?
  • 我刚刚删除了所有data-toggle 属性并修复了。
  • @TRiNE - 也为我做了!谢谢
【解决方案2】:

好的,你必须删除斜线,'/',你的 html 变成了,

<a href="#about" title="About Us">About</a>

如果你想保留斜线,'/'你必须使用data-target属性,

<a href="#/about" title="About Us" data-target="#about">About</a>

更多信息here

【讨论】:

  • 你指的是标签吗? - 它不是一个引导选项卡,它只是一个普通的链接
  • 你试过了吗?你放&lt;div ng-view=""&gt;&lt;/div&gt;了吗??
  • 对不起,你让我把 ng-view 放在哪里,它是标题,我使用了 ng-include
  • 在使用路由时最好使用 ng-view。您通常会将它放在您的 index.html 中,您将在其中注入视图,与路由形成 SPA 非常吻合。不管怎样,你修好了吗?
【解决方案3】:

这个问题也发生在 Angular 2 项目上。我只是将 data-target="#" 添加到锚链接。它解决了我的问题。

 &lt;a data-target="#" [routerLink]="['/link']"&gt;my link&lt;/a&gt;. 

【讨论】:

  • 我面临着与您所面临的相同的问题。可以请分享您的代码的示例实现
  • 当我尝试您的解决方案时。路由器链接在新标签而不是同一页面中打开
【解决方案4】:

这可能是一种非常罕见的情况,但由于一些使用 Colorbox 和 location.hash 的旧代码,我遇到了同样的错误。

jQuery(function() {jQuery('.content-row .csc-default a').tooltip({placement:'top'});
jQuery('a.gallery').colorbox({
    maxWidth:'95%',
    maxHeight:'95%',
    slideshow:true,
    current:' {current} / {total}',
    opacity:'0.80',
    transition:'none',
    speed:'550',
    slideshowSpeed:'5500',
    overlayClose:true,
    fixed:false,
    escKey:true,
    arrowKey:true,
    loop:true,
    title: function() { return $(this).data('original-title')},
    close:'<span class="glyphicon glyphicon-remove"></span>',
    previous:'<span class="glyphicon glyphicon-chevron-left"></span>',
    next:'<span class="glyphicon glyphicon-chevron-right"></span>',
    slideshowStart:'<span class="glyphicon glyphicon-play"></span>',
    slideshowStop:'<span class="glyphicon glyphicon-pause"></span>',
    rel: function() { return $(this).data('rel')}
});
if (location.hash) $(location.hash).collapse('show'); //when this line is commented, Angular Route works properly
});

奇怪的是,只有在加载带有视图链接的页面(如 localhost/index.html#/main)或只是在选择任何视图的情况下重新加载页面时才会发生错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-10
    • 2017-06-06
    • 2019-02-18
    • 2017-11-24
    • 2015-11-13
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多