Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 4 files:

  • index.html
  • logic.js
  • controller.js
  • homepage.html

index.html

<html ng-app="sample">
<head>
<script src="angular.js"></script>
</head>
<body>
<div>
    <div ng-view></div>
</div>    
    <script src="controller.js"></script>
    <script src="logic.js"></script>    
</body>
</html>

logic.js

var myapp = angular.module('sample',[]);
    myapp.config(function($routeProvider)
    {
        $routeProvider
            .when('/',
                {   
                    controller:homepageCtrl,
                    templateUrl:'homepage.html'             
            });
    });

controller.js

function homepageCtrl($scope){
        $scope.name = "ROHIT";}

homepage.html

{{name}}

homepage.html is loading and being displayed correctly by the route but the controller is not being called here when homepage.html is loaded into index.html.

Kindly help me out with this.

Thanks

share|improve this question
 
Have you checked console to see if there was an error? If it didn't find your controller then that would explain why it shows up like that. –  Jeremy Reed Oct 9 '13 at 22:24
add comment

2 Answers

Controller name must be pass to $routeProvider as a string :

$routeProvider
            .when('/',
                {   
                    controller:'homepageCtrl',
                    templateUrl:'homepage.html'             
            });
share|improve this answer
 
Thanks but i have already tried this with the quotes controller:'homepageCtrl' but its not working. –  rohit Sep 20 '13 at 10:21
 
@rohit : OK, How do you add your controller to angular ? –  Corum Sep 20 '13 at 12:26
add comment

You didn't defined controller in HTML.

Add this line

<div ng-controller = "homepageCtrl"> 

Suppose it should be in homepage.html:

<div ng-controller = "homepageCtrl">
   {{name}}
</div>

In addition, wrap your controller name with ' logic.js

[EDIT]

Add $inject to routeProvider:

myapp.config(["$routeProvider",
function($routeProvider) {
    $routeProvider
        .when("/", {
            templateUrl: 'homepage.html', 
            controller: 'homepageCtrl'
        });
}
]);
share|improve this answer
 
Thanks.But this is not necessary right as the routing takes care of this in logic.js . I did try it with this as well , but to no avail. –  rohit Sep 20 '13 at 10:26
 
@rohit see my edit, maybe you didn't $inject? I changed a bit your config –  Maxim Shoustin Sep 20 '13 at 10:30
 
I've found the issue,thanks. The $inject is not required and we dont need to specify the controller name too <div ng-controller = "homepageCtrl"> I actually had a couple of extra things in {{ }} that i didn't put up ,angular had some issues parsing it.It worked when i removed it. –  rohit Sep 20 '13 at 10:43
 
please, post your right answer –  Maxim Shoustin Sep 20 '13 at 10:44
 
i happened to have some mustache code with #,/ in the {{ }} which angular couldn't parse and hence it didn't display {{name}} too even though {{name}} was not enclosed with any of the mustache code –  rohit Sep 20 '13 at 10:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.