【问题标题】:How to force Angular to send request to server on HTML5 mode?如何强制 Angular 在 HTML5 模式下向服务器发送请求?
【发布时间】:2013-11-20 10:17:10
【问题描述】:

我正在尝试使用 angular 和 nodejs 构建和 web。我在/home 路径上加载Angular,/ 包含登录和注册表单。这是我的角度配置:

window.app.config(['$routeProvider', '$locationProvider',
    function($routeProvider,$locationProvider) {
        $locationProvider.html5Mode(true);
        $locationProvider.hashPrefix('!');
        $routeProvider.
        when('/profile', {
            templateUrl: '/views/account.html',

        }).
        when('/edit', {
            templateUrl: '/views/edit.html',

        }).
        when('/home', {
            templateUrl: 'views/index.html'
        }).
        when('/signout', {
            templateUrl: 'views/signout.html'
            //on this view i load a controller which submits a form to /signout
        }).

        otherwise({
            redirectTo: '/home'
        });
    }
]);

在服务器端路由:

    app.get('/',function(){
          res.render('index',{
             user: req.user? req.user:'guest'
        });
    });
    app.post('/login',function(){
             //if success redirect to /home
             //if fails redirect to /
    });
    app.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email',                  'user_about_me', 'read_stream', 'publish_actions'], failureRedirect: '/' }));
  app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/' }), users.authCallback);
    app.get('/signout',function(){
             //signing out the user here...and redirects to /
    });
    app.get('/home',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      }); 
    app.get('/profile',function(req,res){
        res.render('users/home',{
          user: req.user? req.user: 'guest',
          message: req.flash('error')

        })
      });
      app.get('/edit',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      });

如果我点击facebook auth,我如何通过点击 url /home 上的链接转到 /auth/facebook 来发送请求,如果我点击 facebook auth 它会将我发送到 /home。 Angular 阻止向服务器发送请求,它只是转到 url /home,我尝试通过替换 / 来删除 /home。结果是一样的,只是那里没有加载视图。

【问题讨论】:

    标签: node.js angularjs oauth oauth-2.0 angular-routing


    【解决方案1】:

    来自Docs

    在以下情况下,链接不会被重写;相反, 浏览器将执行完整页面重新加载到原始链接。

    • 包含目标元素的链接。示例:<a href="/ext/link?a=b" target="_self">link</a>

    • 指向不同域的绝对链接。示例:&lt;a href="http://angularjs.org/"&gt;link&lt;/a&gt;

    • 以“/”开头的链接在定义 base 时会导致不同的基本路径。示例:&lt;a href="/not-my-base/link"&gt;link&lt;/a&gt;

    因此,作为最简单的解决方案,只需将target="_self" 添加到您的链接。

    【讨论】:

    • 你能帮我解决第三点吗?我不明白。但我认为这对我将来可能很重要:)
    • 刚开一个新问题。
    【解决方案2】:

    如果您希望在一处更改路由配置而不是更改所有链接元素,您可以这样做:

    $routeProvider.when('/externalpath', { resolve: {
        redirect: ['$location', function($location) {
            window.location.replace($location.absUrl());
        }]
    }});
    

    这基本上在点击到该路由的链接时调用内联函数,并且该函数只是将浏览器重定向到目标位置(角度之外)。无论是否为路由配置了模板或控制器,都会处理“解析”配置,并且“重定向”名称基本上只是一个虚拟名称 - 重要的是该函数被执行以获取其值(未使用在这种情况下)。

    这也很有用,例如您有一个需要访问服务器的 /signout url。如果您希望所有未配置的路径都由服务器处理,您还可以使用 else() 配置而不是 when()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      相关资源
      最近更新 更多