[英]ASP.NET angularjs redirect to another view of controller
单击按钮Add
后如何打开位于/ Home / CreateItem的视图。 我需要写些什么
$scope.Add = function() {
console.log('working');
}
Index.cshtml:
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/MyScript/Custom.js"></script>
<script src="~/Scripts/angular-animate/angular-animate.min.js"></script>
<script src="~/Scripts/angular-ui/ui-bootstrap.min.js"></script>
<script src="~/Scripts/angular-ui/ui-bootstrap-tpls.min.js"></script>
<div ng-controller="Second">
<button ng-click="Add()">Add</button>
</div>
家用控制器:
namespace MvcApplication6.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult GetData()
{
string data = "From controller";
return Json(data, JsonRequestBehavior.AllowGet);
}
public ActionResult CreateItem()
{
return View();
}
}
}
Controller.js
var app = angular.module('MyApp', ['ngAnimate', 'ui.bootstrap']);
app.controller('Second', function ($scope, sharedProperties) {
$scope.Add = function() {
// How to redirect to Home/CreateItem ?
}
});
好
app.controller('Second', function ($scope, sharedProperties,$location) {
$scope.Add = function() {
$location.path("/Home/CreateItem");
}
});
更好
app.controller('Second',["$http","sharedProperties","$location", function ($scope, sharedProperties,$location) {
$scope.Add = function() {
$location.path("/Home/CreateItem");
}
}]);
更好的方法更好,因为它使您可以最小化角度代码。 通过明确告诉角度引擎您期望$http
作为参数1,并sharedProperties
作为param2,minifier可以将这些变量名称更改为x
和y
从而使您的JavaScript较小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.