0

I am new to angularjs .my cshtml file is :

@{
    //Layout = "~/Views/Shared/_Layout.cshtml";
    Layout = null;
}


<!DOCTYPE html>

<html ng-app="myApp">
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        .gridStyle {
            border: 1px solid #d4d4d4;
            /*width: 400px;
            height: 200px;*/
        }
    </style>
    <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")" type="text/javascript"></script>
    <script type="text/javascript" src="~/Scripts/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js"></script>
</head>
<body ng-controller="MyCtrl">
    <div class="container">
        <script type="text/javascript">
            var app = angular.module('myApp', ['ngGrid']);
            $(document).ready(function () {

                ///////////////////////////DISPLAY LIST/////////////////////////
                $('#DisplayListObj').click(function (e) {
                    $.ajax({
                        type: 'GET',
                        dataType: 'json',
                        contentType: 'application/json',
                        url: '@Url.Action("DisplayListObject", "Demo")',
                        success: function (Product) {
                            //var result = '<table>';
                            //for (var i = 0 ; i < Product.length ; i++) {
                            //    result += "<tr>" + "<td>" + Product[i].Id + "\t</td>" + "<td>" + Product[i].Name + "\t</td>" + "<td>" + Product[i].Company + "\t</td>" + "</tr>";
                            //}
                            //$('#IdresultListDisplay').html(result + '</table>');

                            app.controller('MyCtrl', function ($scope) {
                                $scope.Data = Product;
                                $scope.gridOptions = { data: 'Data' };
                            });
                        }
                    });
                });
                ///////////////////////////DISPLAY LIST/////////////////////////
                $('#Add').click(function (event) {


                    var ID = $('#ID').val();
                    var Name = $('#Name').val();
                    var Company = $('#Company').val();
                    $.ajax({
                        type: 'GET',
                        dataType: 'json',
                        data: { ID: ID, Name: Name, Company: Company },
                        contentType: 'application/json',
                        url: '@Url.Action("DisplayObject", "Demo")',
                        success: function (Product) {
                            var result = '<table>';
                            for (var i = 0 ; i < Product.length ; i++) {
                                result += "<tr>" + "<td>" + Product[i].Id + "\t</td>" + "<td>" + Product[i].Name + "\t</td>" + "<td>" + Product[i].Company + "\t</td>" + "</tr>";
                            }
                            $('#IdresultListDisplay').html(result + '</table>');

                        }
                    });
                });
            });

        </script>

        <h3 style="color:white">Display List using JSON @DateTime.Now</h3>
        <a href="#" class="btn btn-default btn-lg" id="DisplayListObj">Display List Object</a>
        <br />
        <br />
        @*<div id="IdresultListDisplay" style="color:white"></div>*@
        <div class="gridStyle" ng-grid="gridOptions"></div>
        <br />
        <br />
        <div id="AddNew">
            <input type="text" id="ID" value="4" name="ID" />
            <br />
            <input type="text" id="Name" value="Name" name="Name" />
            <br />
            <input type="text" id="Company" value="Company" name="Company" />
            <br />
            <a href="#" class="btn btn-default btn-lg" id="Add">Add New</a>
            <br />
        </div>
    </div>
</body>

</html>

and my controller methods are like :

public class DemoController : Controller
    {
        // GET: Demo

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult HelloAjax()
        {
            return Content("Hello Ajax","text/plain");
        }
        public ActionResult Sum(int a , int b)
        {
            return Content((a+b).ToString(), "text/plain");
        }
        public ActionResult DisplayObject(string ID , string Name , string Company)
        {
            mydbEntities ProductEntity = new mydbEntities();
            Product P = new Product();
            P.Id = ID;
            P.Name = Name;
            P.Company = Company;
            ProductEntity.Products.Add(P);
            ProductEntity.SaveChanges();
            return Json(ProductEntity.Products.ToList(), JsonRequestBehavior.AllowGet);
        }
        public ActionResult DisplayListObject()
        {
            mydbEntities ProductEntity = new mydbEntities();
            return Json(ProductEntity.Products.ToList(), JsonRequestBehavior.AllowGet);
        }
    }

i am getting this error in my F12 console window:

angular.min.js:6 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.21/$injector/modulerr?p0=myApp&p1=Error%3A%…%20c%20(http%3A%2F%2Flocalhost%3A5760%2FScripts%2Fangular.min.js%3A18%3A60)

5
  • You have not defined a module myApp define that first. Commented Mar 31, 2016 at 6:01
  • Hello , i have defined it in html tag and in script tag below var app = angular.module('myApp', ['ngGrid']);. what else i have to do? Commented Mar 31, 2016 at 7:50
  • Have you checked that on body you should have "DemoController" or "MyCtrl"? Commented Mar 31, 2016 at 8:37
  • i have my body tag as <body ng-controller="MyCtrl"> Commented Mar 31, 2016 at 8:57
  • why using jquery click if you are making an AngularJs app you should use ng-click instead. code will be easy to understand then Commented Mar 31, 2016 at 9:23

3 Answers 3

0

I think you are missing the script src for ngGrid. The error is a link, you can click it to see exactly what module you are missing.

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

1 Comment

Thanks for a quick reply . if i click on the link it redirects me to this link "docs.angularjs.org/error/$injector/…". it has something to do with ngRoute.
0

In the main module u have injected dependency of ngGrid module. So u need to include the js file which have ngGrid module. Include below files in the head section

https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js

Also include css file for style

https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css

For more information regarding ngGrid Module go through this link

https://github.com/angular-ui/ui-grid

4 Comments

Hi , I have done what you have proposed but i am getting the same error. its something to do with ngroute.
Sorry.. Now ngGrid is deprecated.. u should use ui.grid module of angularJs
ui.grid has replaced ngGrid, and ngGrid has been deprecated. Your answer DID help me figure out my problem though, thanks. please recommend any beginners video tutorials for ui.grid if you know of any.
I have no idea about the live tutorials but u can check the ui-grid live demo at this link: jsfiddle.net/relly/ysmg1qkt
0

You have used "MyCtrl" on body but your "MyCtrl" is instanciated in click function of "#DisplayListObj" element and thats the problem.

Define your "MyCtrl"outside click function

1 Comment

Hi there ! thank you for your time. i moved my code app.controller('MyCtrl', function ($scope) { $scope.Data = Product; $scope.gridOptions = { data: 'Data' }; }); from click event and placed it under this line var app = angular.module('myApp', ['ngGrid']); but still i am getting the same error.Also i tried moving it into seperate script tag in header but , again , same error

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.