角UI的路由器发送根URL

角UI的路由器发送根URL

本文介绍了角UI的路由器发送根URL 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于真气问题 UI路由器。一切正常,因为我想,所有不良网址发送到 404 状态。不过,即使我的默认状态是正确呈现当网址 /#/ / 被重定向的URL到 / 404 / 。哪能服务器默认状态,以两个 / /#/

I am having an infuriating issue regarding ui-router. Everything works as I want, where all bad urls are sent to the 404 state. However, even though my default state is correctly rendered when the url is /#/, the url of / is redirected to /404/. How can I server the default state to both / and /#/?

app.js

MyApp.config( function ($stateProvider, $urlRouterProvider) {

// For any unmatched url, send to 404
$urlRouterProvider.otherwise("/404/");

$stateProvider
    // Home  (default)
    .state('default', {
        url: '/',
        resolve: {
            ...
        },
   }
});

任何帮助非常AP preciated。

Any help much appreciated.

推荐答案

我认为这将实现您的需求 -

I think this will accomplish your needs -

MyApp.config(function($stateProvider, $urlRouterProvider) {
    // the known route
    $urlRouterProvider.when('', '/');

    // For any unmatched url, send to 404
    $urlRouterProvider.otherwise('/404');

    $stateProvider
        // Home  (default)
        .state('default', {
            url: '/',
            resolve: {
                // ...
            }
            // ....
        });
});

我来所指向。您可能需要使用常规的前pression来处理与数据路由。

I refered to this post. You may need to use a regular expression to handle routes with data.

相反的在您的网址,你可以使用查询字符串太,并通过 $ stateParams 抓住它的状态。

Instead of the # in your URL you can use a query string too and grab it via $stateParams in a state.

下面是你如何做到这一点 -

Here is how you can do that -

  // ....

  $stateProvider

    .state('default', {
        url: '/?data',
        templateUrl: 'templates/index.html',
        controller: 'defaultCtrl'
    })

然后你可以使用这个下面去家里用的数据 -

And then you can use this below to go to home with data -

var toStateData = {
    key1: 'value1',
    key2: 'value2'
}

$state.go('default', {data: JSON.stringify(toStateData)});

您不必在字符串化 $ stateParams 数据,但是这将允许用一个参数更多的选择。在 defaultCtrl 控制器就可以得到通过查询字符串这样的 -

You don't have to stringify the data in $stateParams but this will allow more options with one parameter. In the defaultCtrl controller you can get the passed query string like this -

var stateData = JSON.parse($stateParams.data);
var key1 = stateData.key1;
// ....

这篇关于角UI的路由器发送根URL 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:30