【问题标题】:ASP.NET angularjs redirect to another view of controllerASP.NET angularjs 重定向到控制器的另一个视图
【发布时间】:2016-05-02 20:31:23
【问题描述】:

点击按钮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 ?
    }
});

【问题讨论】:

    标签: javascript asp.net angularjs


    【解决方案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");
        }
    }]);
    

    Better 方式更好,因为它可以让您缩小 Angular 代码。通过明确告诉 Angular 引擎您期望 $http 作为参数 1 和 sharedProperties 作为参数 2,缩小器可以将这些变量名称更改为 x 和 y 从而使您的 javascript 更小。

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多