ASP.NET angularjs重定向到控制器的另一个视图

ASP.NET angularjs redirect to another view of controller

本文关键字:另一个 视图 控制器 NET angularjs 重定向 ASP      更新时间:2023-09-26

单击按钮Add后如何打开位于/Home/CreateItem的视图。我需要在中写什么

$scope.Add = function() {
        console.log('working');
    }

Index.chtml:

<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 ?
    }
});

OK

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");
    }
}]);

Better的方式更好,因为它使您能够缩小角度代码。通过明确告诉角度引擎您期望$http作为参数1,sharedProperties作为参数2,迷你程序可以将这些变量名更改为xy,从而使您的javascript更小。