导航链接时,AngularJS视图不会更改 [英] AngularJS view doesn't change when navigating to link

查看:125
本文介绍了导航链接时,AngularJS视图不会更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被莫名其妙如下:我有这样一个简单的链接:

 <李>< A HREF =#/ foo的> FOOS< / A>< /李>

但它点击的时候,视图不会更新。在浏览器的URL变化,但没有任何反应,没有东西显示在控制台中。如果我直接在浏览器中加载的URL,那么正确的页面加载。

该路线是这样的:

 的app.config(函数($ routeProvider,RestangularProvider){
    $ routeProvider
        。当('/指数',{templateUrl:资产/视图/ index.html的'})
        。当('/ FOOS',{templateUrl:资产/视图/ list.html',控制器:controllers.FooListCtrl})
        。当('/ FOOS /创建',{templateUrl:资产/视图/ update.html',控制器:controllers.FooUpdateCtrl})
        。当('/ FOOS /:ID /更新',{templateUrl:资产/视图/ update.html',控制器:controllers.FooUpdateCtrl})
        不然的话({redirectTo:'/指数'});    RestangularProvider.setBaseUrl(/ admin的);
});


解决方案

好了,显然,这是一个<一个href=\"https://groups.google.com/forum/#!searchin/angular/link%2420not%2420working/angular/BsenavakfyQ/wtbRBVBxJ0sJ\"相对=nofollow>各种各样的已知问题的:当locationProvider(即 $位置)注入到一个控制器,链接停止工作全局。一种解决方法似乎是重写通过jQuery的联系,以及作品:

  app.run(函数($ rootScope){
    $('[NG-APP]')。在('点击','A',函数(){
        window.location.href = $(本).attr('href属性);
    });
});

I'm baffled by the following: I have a simple link like this:

    <li><a href="#/foo">Foos</a></li>

but when clicking on it, the view is not updated. The URL in the browser changes, but nothing happens, and nothing gets displayed in the console. If I load the URL directly in the browser, then the correct page is loaded.

The routes look like this:

app.config(function($routeProvider, RestangularProvider) {
    $routeProvider
        .when('/index', {templateUrl: 'assets/views/index.html'})
        .when('/foos', {templateUrl: 'assets/views/list.html', controller: controllers.FooListCtrl})
        .when('/foos/create', {templateUrl: 'assets/views/update.html', controller: controllers.FooUpdateCtrl})
        .when('/foos/:id/update', {templateUrl: 'assets/views/update.html', controller: controllers.FooUpdateCtrl})
        .otherwise({redirectTo: '/index'});

    RestangularProvider.setBaseUrl("/admin");
});

解决方案

Ok, so apparently, this is a known issue of sorts: when the locationProvider (i.e. $location) is injected to one controller, links cease to work globally. One workaround seems to be to rewrite the links via jQuery, and that works:

app.run(function($rootScope) {
    $('[ng-app]').on('click', 'a', function() {
        window.location.href = $(this).attr('href');
    });
});

这篇关于导航链接时,AngularJS视图不会更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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