AngularJS是一个前端JavaScript框架,而MVC(Model-View-Controller)通常指后端框架(如ASP.NET MVC)。要从AngularJS控制器重定向到MVC视图,需要理解两者之间的交互机制。
angular.module('myApp').controller('MyController', function($scope) {
$scope.redirectToMvcView = function() {
window.location.href = '/ControllerName/ActionName';
};
});
angular.module('myApp').controller('MyController', function($scope, $window) {
$scope.redirectToMvcView = function() {
$window.location.href = '/ControllerName/ActionName';
};
});
$http.post('/api/redirect', { someData: data })
.then(function(response) {
window.location.href = response.data.redirectUrl;
});
public ActionResult GetRedirectUrl()
{
// 处理逻辑
return Json(new { redirectUrl = Url.Action("ActionName", "ControllerName") });
}
AngularJS控制器:
angular.module('app').controller('OrderController', ['$scope', '$window', '$http',
function($scope, $window, $http) {
$scope.submitOrder = function() {
$http.post('/Order/Submit', $scope.order)
.then(function(response) {
$window.location.href = '/Order/Confirmation/' + response.data.orderId;
});
};
}
]);
MVC控制器:
public class OrderController : Controller
{
[HttpPost]
public ActionResult Submit(OrderModel order)
{
// 处理订单逻辑
int orderId = SaveOrder(order);
return Json(new { orderId = orderId });
}
public ActionResult Confirmation(int orderId)
{
var model = GetOrderConfirmation(orderId);
return View(model);
}
}
这种方法确保了前后端的清晰分离,同时实现了从AngularJS到MVC视图的无缝重定向。