0

I am trying to develop a simple web application using angularjs i have a table city that is created using entityframework database first.

I added angularjs nuget to my project.and create 2 files in my project named service and controller as you can see here :

controller

MyApp.controller('UpdateController', function ($scope, CityService) {

    getCities();

    function getCities() {

        CityService.getCities()

            .success(function (cities) {

                $scope.cities = cities;

            })

            .error(function (error) {

                $scope.status = 'Unable to load customer data: ' + error.message;

            });

    }

});

Service :

MyApp.factory('CityService', ['$http', function ($http) 
{
    var urlBase = 'http://localhost:37404/api';

    var CityService = {};

    CityService.getCities = function () {

        return $http.get(urlBase + '/default1');

    };

    return CityService;

}]);

I pass the data using webapi as you can see :

namespace MvcApplication8.Controllers
{
    public class Default1Controller : ApiController
    {
        private testDBEntities db = new testDBEntities();

        // GET: api/ManageStudentsInfoAPI  
        public IQueryable<City> GetStudent()
        {
            return db.Cities;
        }   
    }
}

Here is my view :

<header>
    <div class="content-wrapper">
        <div class="float-left">
            <p class="site-title">
                <a href="~/">ASP.NET Web API</a></p>
        </div>
    </div>
    <script src="~/Scripts/jquery-1.7.1.js"></script>
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/MyAngularFile/Service.js"></script>
    <script src="~/MyAngularFile/Controller.js"></script>
</header>
<div id="body">
    <div ng-controller="UpdateController">    
        <table class="table">    
            <tr> 

                <th>Id</th>

                <th>Name</th>

                <th>Country</th>

            </tr>

            <tr ng-repeat="a in cities">

                <td>{{a.Id}}</td>

                <td>{{a.Name}}</td>

                <td>{{a.Country}}</td>

           </tr>

        </table>
    </div>
</div>

But the result is like this :

enter image description here

My browser errors:

enter image description here

4
  • Is ng-app specified in the html code? Are there any errors in the browser console (Ctrl+shift+i to bring it up)? Commented Apr 6, 2016 at 6:53
  • @Petter i don't have any tag in my html code named ng-app .i added the error list to my post Commented Apr 6, 2016 at 6:56
  • 1
    Ok, you need an ng-app="MyApp" attribute, for example in the body tag. Also, the browser console complains that MyApp hasn't been defined. You probably need to add var MyApp = angular.module('MyApp', []); in the Service.js file. Commented Apr 6, 2016 at 7:01
  • You could check a sample plunker : plnkr.co/edit/FcgUs3NMTa6lsNDLLhKR?p=preview Commented Apr 6, 2016 at 7:25

1 Answer 1

0

Try this:

var MyApp = angular.module('MyApp',[]);

Then, in html:

<div id="body" ng-app="MyApp">
Sign up to request clarification or add additional context in comments.

5 Comments

i get this error :Error: [$injector:modulerr] Failed to instantiate module MyApp due to: [$injector:nomod] Module 'MyApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. errors.angularjs.org/1.5.3/$injector/nomod?p0=MyApp
Where should i add this line var MyApp = angular.module('MyApp',[]);
before your controller/service implementation
can i add the var MyApp = angular.module('MyApp',[]); in another javascript file named module ?
Try adding a script app.js with contents var MyApp = angular.module('MyApp',[]);. Then, in both Service.js and Controller.js, add this on top of the script: var MyApp = angular.module('MyApp');

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.