是否有可能在angularjs的發佈請求後重定向到另一個視圖SecondView()
。我知道角有點像$location.path
。但我想檢查控制器中的一些條件。AngularJS發佈到控制器並重定向到另一個頁面
的JavaScript
var MyApp = angular.module("MyApp", []);
MyApp.controller("AppController", function ($scope,$http) {
$scope.onClick = function() {
$http({
method: 'POST',
url: 'Home/CheckData/',
data: {name: 'Jhon'},
});
};
});
控制器:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CheckData(string name)
{
if (name=="Jhon")
{
return RedirectToAction("SecondView");}
else{
return View();
}
}
public ActionResult SecondView()
{
return View();
}
}
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/MyScript.js"></script>
<div ng-app="MyApp" ng-controller="AppController">
<input ng-click="onClick()" type="button" value="Redirect" />
</div>
實際上,服務器應該總是響應客戶端,爲什麼不把這個響應解析成有意義的東西(比如url重定向?),請檢查@charlietfl的答案。 –
我很確定兩個答案都達到了相同的點:讓服務器以某種有效負載作出響應,然後基於此負載重定向或執行其他操作。 (爲了說明的目的,當我指出哪個控制器應該處理重定向時,我只是添加了Angular這個詞 - 當你在同一句話中談論Web API和Angular時,事情往往會變得模糊不清。) – napo