我想將部分視圖(_Test.cshtml)加載到我的產品控制器的Index視圖中。Angular ng-view在MVC局部視圖中不起作用
但是局部視圖在新頁面打開,我在這裏和谷歌搜索了很多。但我似乎無法弄清楚我做錯了什麼?任何幫助真的很感激!
MVC控制器
public ActionResult Test()
{
return PartialView("~/Views/Product/_Test.cshtml");
}
//this returns the partial view
public ActionResult TestData()
{
List<Product> products = new List<Product>();
var db = new HotStuffPizzaContext();
var result = (from p in db.Products
select p)
.Where(x => x.ProductID != 0)
.Select(a => new
{
ProductID = a.ProductID,
Description = a.Description
});
return Json(result.ToList(), JsonRequestBehavior.AllowGet);
}
//這是通過角控制器
角腳本
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider.
when('/Product', {
templateUrl: '/Product/Index',
controller: 'testCtrl'
}).
when('Product/Test', {
templateUrl: '/Views/Product/_Test',
controller: 'testCtrl'
}).otherwise({
redirectTo: '/'
});
//$locationProvider.html5Mode(true);
//$locationProvider.hashPrefix('!').html5Mode(true);
//$locationProvider.html5Mode(false).hashPrefix("!");
}]);
myApp.controller('testCtrl', ['$scope', '$http',
function ($scope, $http) {
$http.get('/Product/TestData').success(function (data) {
$scope.products = data;
});
}]);
產品/ Index.cshtml
稱爲@{
ViewBag.Title = "Index";
}
索引視圖
<a href="Product/Test">Test</a>
<div ng-view="">
</div>
管窺
@{ Layout = null; }
<html data-ng-app="myApp">
<body data-ng-controller="testCtrl">
<div>
<table class="table table-striped">
<tr >
<th>id</th>
<th>description</th>
</tr>
<tr data-ng-repeat="p in products">
<td>{{p.ProductID}}</td>
<td>{{p.Description}}</td>
</tr>
</table>
</div>
</body>
</html>
<script src="../../Scripts/angular.min.js"></script>
<script src="../../Scripts/angular-route.js"></script>
<script src="../../Scripts/App/app.js"></script>
我也刪除來自於我的應用包羅萬象開始routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "product",
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "product",
// url: "{product}/{*catchall}",
// defaults: new { controller = "Product", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }) ;
}
}
您是否正在嘗試將角度應用加載到您的MVC應用程序的容器中?即索引是不是一個角度的應用程序,但部分將是兒童角度的應用程序?或者這是一個MVC後端角度的應用程序和索引頁面始終配置爲角? – Jasen
角度應用程序有一個mvc後端。因此,mvc將爲每個控制器提供數據和頂層視圖。但是Product/angular路徑之後的任何東西都應該接管並在ng視圖中顯示部分html – Haris
不刪除CATCH ALL使MVC完成工作(而不是Angular)? –