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 am trying to write a small web application: listing user name and number

Those files are in the same folder, for example:

ang8
  |----  index.html
  |----  list.html
  |----  contacts.js   

When I am trying to open index.html, it does not load the content in list.html. Any idea?? Thanks!

The index.html code is:

<!DOCTYPE html>
<html ng-app="contacts">
    <meta character="utf-8">
    <title>Contacts</title>
    <style type="text/css">
        * { box-sizing: border-box; }
        body { font: 14px/1.5 sans-serif; color: #222; margin: 3em;}
    </style>

    <div ng-controller="Contacts">
        <h1>Contacts</h1>
        <div ng-view></div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
    <script src="contacts.js"></script>
</html>

The list.html code:

<h2>List</h2>
<div>
    <label>search: </label><input type="search" ng-model="search">
    <br>
</div>
<ul>
    <li ng-repeat="contact in contacts | filter:search">
        <a href="#/contact/{{$index}}">{{contact.name}}</a>
        : {{contact.number}}
    </li>
</ul>

and the js code:

angular
    .module('contacts', [])
    .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'list.html'
            });
    })
    .controller('Contacts', function($scope){
        $scope.contacts = [
            {name: 'Tom', number: '1234'},
            {name: 'David', number: '4321'},
            {name: 'Jason', number: '12345'}
        ];
    })
share|improve this question
    
Where are you including the source for ngRoute? –  Asok Feb 27 '14 at 13:36
    
@Asok sorry, there is no ngRoute, type mistake –  Jusleong Feb 27 '14 at 13:38
    
So you are still getting the injection error without ngRoute, correct? Just making sure. Actually, if I am correct you need ngRoute to access $routeProvider, so you could either reference ngRoute or remove your $routeProvider and you should be fine –  Asok Feb 27 '14 at 13:39
    
If there's no ngRoute why are you injecting $routeProvider? –  khellang Feb 27 '14 at 13:40
1  
Both ng-view and $routeProvider is in the ngRoute module. So either you have to add the dependency and .js reference, or you have to remove both. –  khellang Feb 27 '14 at 13:42

1 Answer 1

up vote 2 down vote accepted

Angular can't find the ngRoute dependency. You're missing a reference to angular-route(.min).js. Add the following line below the angular.min.js reference:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-route.min.js"></script>
share|improve this answer
    
Thanks! It works! –  Jusleong Feb 27 '14 at 13:42
    
Then vote up and mark as answer! –  petur Feb 27 '14 at 13:44
    
And BTW, you're missing the controller: 'Contacts' in your route declaration :) –  khellang Feb 27 '14 at 13:44
    
To add to @khellang's last comment, he is correct, the controller can and should really be declared in the route config, which means you wouldn't really need any controller declaration in the html, unless you were nesting controllers of course –  Asok Feb 27 '14 at 13:47
    
See my answer guy's –  Ramesh Rajendran Feb 27 '14 at 13:50

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.