0

So I have my index page:

<!DOCTYPE html>
<html ng-app="application" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="app/app.js"></script>
</head>
<body>

    <!-- Place where the page will be rendered at -->
    <div ng-view>
    </div>

</body>
</html>

The application looks like this:

var application = angular.module('application', ["ngRoute"]);

application.config(["$routeProvider", "$locationProvider",
    function ($routeProvider, $locationProvider) {
        $routeProvider
            .when("/home", {
                templateUrl: "app/ListContactsForm/lcTemplate.html",
                controller: "HomeController"
            })
            .otherwise({
                redirectTo: "/home"
            });
        //$locationProvider.html5Mode(true);
    }]);



application.controller("HomeController",
    ["$scope", "$location", "DataService",
    function ($scope, $location, DataService) {

        alert("home hit!");

    }]);

The lcTemplate.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>aaaa</title>
</head>
<body>
    I'm here!!
</body>
</html>

The problem is that the lcTemplate is rendered, i get the "I'm here!!" message, but he HomeController function is never called. Why is that so?

3
  • 1
    any error in console..?? Commented Feb 7, 2015 at 12:25
  • I solved it, injection error! Commented Feb 7, 2015 at 12:27
  • 1
    @omegasbk Just answer your own question, it may help someone in the future ;) Commented Feb 7, 2015 at 12:28

3 Answers 3

1

The problem was in injecting the DataService which did not exist (I removed the service).

Error: [$injector:unpr] http://errors.angularjs.org/1.3.12/$injector/unpr?p0=DataServiceProvider%20%3C-%20DataService%20%3C-%20HomeController

Changes which needed to be done were all in the application code:

var application = angular.module('application', ["ngRoute"]);

application.config(["$routeProvider", "$locationProvider",
    function ($routeProvider, $locationProvider) {
        $routeProvider
            .when("/home", {
                templateUrl: "app/ListContactsForm/lcTemplate.html",
                controller: "HomeController"
            })
            .otherwise({
                redirectTo: "/home"
            });
        //$locationProvider.html5Mode(true);
    }]);



application.controller("HomeController",
    ["$scope", "$location",
    function ($scope, $location) {

        alert("home hit!");

    }]);
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, if you showed the error on the question, it should be easy to spot the error.
Yes, I am still not used to using the console in the browser since I am pretty new at this. :)
1

You have to remove the complete HTML definition from the template because the view is rendered inside

 <div ng-view>
    </div>

lcTemplate.html: should be like

<div>I'm here!!</div>

1 Comment

That did not cause the error, but it is true! Thank you
0

You have not declare "DataService". for example: I have my services in model_services.js

var services = angular.module('ModelServices');

services.factory('DataService', ['$resource', function($resource){
 var DataService = $resource('/api/data/:id', {id: "@id" });

 return DataService;
}]);

This method is for call to any consuming services. '/api/data/:id' is your API Rest path.

After that you have to add your module in your config application, for this example 'ModelServices'

var application = angular.module('application', ["ngRoute", "ModelServices"]);

Now you can call your "DataService" from the controller

application.controller("HomeController",
    ["$scope", "$location", "DataService",
    function ($scope, $location, DataService) {

        console.log("home hit!");

 }]);

I have not idea if you can call "alert" from controller but I'm sure can you use console.log instead.

As you say @omegasbk "The problem was in injecting the DataService which did not exist (I removed the service)." if you have not declare your module that contain your "DataService" object in your application then you can not use this in your controller.

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.