【问题标题】:ui-router, can't navigate to another routeui-router,无法导航到另一条路线
【发布时间】:2018-08-04 20:27:22
【问题描述】:

我在 Mvc 应用程序中遇到 angularJs ui-routing 问题。我已经看到了几个关于这个问题的问题。 This question 似乎特别在解决我的问题。但是我无法导航到不同的路线。

在我的视图/主页/Index.cshtml 中:

 @{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<base href="/">

<h1> Index Page</h1>

<a ui-sref="login">login</a>

<ui-view></ui-view>

在我的 Angular/app.js 中:

  var myApp = angular.module("myApp", ['ui.router']);

myApp.controller("LoginController", LoginController);


 myApp.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {

     $stateProvider
         .state('login', {
             url: '/Login',
             templateUrl: '/home/Login',
             controller: LoginController,
             controllerAs: 'controller'
         })

            .state('Index', {
                url: '/',
                templateUrl: '/home/index'
            })
     // Handle request for non-existent route
     //$urlRouterProvider.otherwise('/home');

     $locationProvider.html5Mode({
         enabled: true,
         requireBase: false
     });


 });

在 Login.cshtml 中:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
} 
    <p> Login Page</p>  

<a ui-sref="Index">Index</a>

Route.config

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

在我看来/shared/_layout.cshtml:

<body ng-app="myApp">

可能相关的问题:

$locationProvider.html5Mode(true) 和 $locationProvider.hashPrefix('!').html5Mode(true) 如何适合图片?

【问题讨论】:

    标签: angularjs asp.net-mvc angular-ui-router


    【解决方案1】:

    您正在使用href 将自己导航到登录状态,这是错误的。您应该改用ui-sref 指令

    <a ui-sref="login">login</a>
    

    您还应该使用ui-view 指令来显示您的目标状态。

    官方文档包含short, simple tutorials,可帮助您入门。

    【讨论】:

    • 什么错误?另外,您是否在服务器和客户端中定义路由?
    • 我遇到了一个可能相关的不同问题。我已经问了here 作为单独的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2020-07-31
    • 2020-01-14
    • 2016-02-19
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多