0

I use the following code to create an angular app, using routes and controllers.

(function() {
    angular.module('eCommerceApp', ['ngRoute'])
        .config('$routeProvider', function($routeProvider) {
            $routeProvider.
                when('/', {
                    templateUrl: 'partials/phonelist.html',
                    controller: 'mobilePhoneListController'
                }).
                when('/phone/:phoneSlug', {
                    templateUrl: 'partials/phonedetail.html',
                    controller: 'mobilePhoneDetailController'
                }).
                otherwise({
                    templateUrl: 'error/404.html',
                });
        })
        .controller('mobilePhoneListController', ['$http', function($http) {
            var thisObj = this;
            thisObj.mobilePhones = [];

            $http.get('/api/getallphones').then( function(data) {
                thisObj.mobilePhones = data;
            }, function(data) {
                thisObj.mobilePhones = data || "Request Data Fail!";
            });
        }])
        .controller('mobilePhoneDetailController', ['$http', function($http) {
            var thisObj = this;
        }])
})();

Before that I imported 3 scripts. Angular, angular-route and my app

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
<script src="/angular/e-commerce-app.js"></script>

and the website main structure

<html lang="en" ng-app="eCommerceApp">
<!-- ... -->
<body ng-view>
</body>
</html>

also tried with <ng-view></ng-view>

But I always seam to get this error.

Error: $injector:modulerr Module Error Failed to instantiate module eCommerceApp due to: Error: [ng:areq] http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20... at Error (native) at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:6:416 ...

2 Answers 2

1

In your website main structure (index.html), use ng-app="eCommerceApp" as an attribute in 'body' tag. For example:

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

3 Comments

sorry, I forgot to put that in, but I already have ng-app in html, I'll edit now. The app worked all until I implemented routes.
This question is answered in this post. Check it. Might be useful for you. Thanks stackoverflow.com/questions/20363020/…
Nah, I figured it out, I'll leave an answer in case someone runs into this problem as well.
0

The problem was that I didn't include the .config file right. I implemented it using

.config('$routeProvider', function($routeProvider) { ... });

while it should've been

.config(['$routeProvider', function($routeProvider) { ... }]);

(notice the [])

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.