0

How after click on button Add open View located at /Home/CreateItem. What i need to write in

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

1

OK

app.controller('Second', function ($scope, sharedProperties,$location) {
    $scope.Add = function() {
$location.path("/Home/CreateItem");
    }
});

BETTER

app.controller('Second',["$http","sharedProperties","$location", function ($scope, sharedProperties,$location) {
    $scope.Add = function() {
$location.path("/Home/CreateItem");
    }
}]);

The Better way is better, because it enables you to minify your angular code. By expliciting telling the angular engine that you are expecting $http as param 1 and sharedProperties as param2, a minifier can change those variable names to x and y thus making your javascript smaller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.