【发布时间】:2016-03-23 10:12:47
【问题描述】:
我是 angularjs 的新手,并在 ASP.NET MVC 中实现它。我阅读了几篇文章并实现了如下代码,
这是我的布局页面,
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>angular demo</title>
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script>
var schoolModule = angular.module("schoolModule", ['ngRoute']);
schoolModule.config(function ($routeProvider) {
$routeProvider.when('/one', {
templateUrl: "Master/One",
controller: "OneController"
})
.when('/two', {
templateUrl: "Master/Two",
controller: "TwoController"
})
.when('/three', {
templateUrl: "Master/Three",
controller: "ThreeController"
})
.otherwise({
redirectTo: 'Master/One'
});
});
schoolModule.controller("OneController", function ($scope) {
$scope.message = "One message";
});
schoolModule.controller("TwoController", function ($scope) {
$scope.message = "Two message";
});
schoolModule.controller("ThreeController", function ($scope) {
$scope.message = "Three message";
});
</script>
</head>
<body ng-app="schoolModule">
<header>Title</header>
@RenderBody()
<footer>Footer here</footer>
</body>
</html>
我的索引页面是,
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Master.cshtml";
}
<a href="/#/one">One</a>
<a href="/#/two">Two</a>
<a href="/#/three">Three</a>
<div ng-view>
</div>
我还创建了 3 个局部视图,只有消息。
<div ng-controller="OneController">
{{message}}
</div>
这里,当我运行项目时,URL 是,
http://localhost:52211/Master/index#/Master/one
当我点击第二个按钮时,它会将我重定向到不同的控制器和操作,但 url 是,
http://localhost:52211/#/two
它会跳转到 Home 控制器,索引操作。
请告诉我我在这里犯了什么错误,以及如何使它起作用? 谢谢。
【问题讨论】:
标签: angularjs asp.net-mvc-4 angular-ui-router asp.net-mvc-routing angularjs-routing