2016-05-02 92 views
0

如何在點擊按鈕Add後打開位於/ Home/CreateItem的視圖。我需要在ASP.NET angularjs重定向到控制器的另一個視圖

$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

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

更好方式比較好,因爲它可以讓你來縮小你的角碼

。通過明確告訴角引擎,您期望$http作爲參數1和sharedProperties作爲參數2,縮小器可以將這些變量名更改爲xy,從而縮小您的JavaScript。

相關問題