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

时间:2016-05-02 20:31:23

标签: javascript asp.net angularjs

点击按钮Add后如何打开位于/ Home / CreateItem的View。我需要写什么

$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>

HomeController中:

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

1 个答案:

答案 0 :(得分:1)

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作为参数2,缩小器可以将这些变量名称更改为xy从而使您的javascript成为可能小。