-1

So I'm trying to add routes to my angularJS app, and I'm running intro a problem where the main controller for my app stops working if I try to add 'ngRoute' to the app. Additionally, ngRoute is not working.

My code:

app.js (angular logic)

(function() {

    angular.module('fccNight', ['ngRoute'])

    .config(['$routeProvider', function($routeProvider) {

        $routeProvider

        .when('/', {
            templateUrl: 'results.html'
        })
        .otherwise({ redirectTo: '/' });

    }])




    //controllers
    .controller('mainController', ['$scope', '$http', function($scope, $http) {

        $scope.searchTerm = '';
        $scope.data = [];
        $scope.loading = false;
        $scope.goingNo = [];

        $scope.getResults = function() {
            if($scope.searchTerm !== '') {
                $scope.loading = true;
                $scope.data = [];
                $http.get('/api/search?term=' + $scope.searchTerm)
                    .success(function(results) {
                        $scope.loading = false;
                        $scope.data = results;
                        for(var i = 0; i < $scope.data.length; i++) {
                            $scope.goingNo.push($scope.data[i].going);
                        }
                    });
            }
        }

        $scope.addGoing = function(index) {
            // check if user is authenticated
            if($scope.data[index].going > $scope.goingNo[index]) {
                $scope.data[index].going -= 1;
            }
            else {
                $scope.data[index].going += 1;
            }
            // register in db
        }

    }])

})();

index.html's body:

<body ng-controller="mainController" id="mainController">





    <!-- CONTENT -->

    <div class="container">

        <div class="row text-center">

            <div class="col-xs-12 main">
                <h1>Where are you going tonight?</h1>
                <p>Find nearby pubs and RSVP ahead of time.</p>

            </div>

        </div>
        <div class="row">

            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-8 col-lg-offset-2">

                <form>

                    <div class="input-group">
                        <input id="inputSearch" ng-model="searchTerm" type="text" class="form-control"  placeholder="Where are you?" autocomplete='off'>

                        <span id="goBtn" class="input-group-btn">
                            <a ng-click="getResults()" class="btn btn-default">Go</a>
                        </span>
                    </div>

                </form>

            </div>

        </div>

        <!-- LOADING -->
        <div class="row loading hidden" ng-show="loading">
            <div class="col-xs-12">

                <div class="loader"></div>

            </div>
        </div>

        <div class="text-center">Controller is working if search term is displayed: {{ searchTerm }}</div>


        <!-- TEMPLATE -->
        <div ng-view></div>


    </div>

    <!-- footer -->
    <footer class="row-footer">

        <div class="container">

            <div class="row">

                <div class="col-xs-12 text-center">

                    <a href="https://github.com/otmeek/fcc-night">
                        <i class="fa fa-github"></i>
                    </a> | 
                    <a href="https://www.freecodecamp.com">
                        <i class="fa fa-fire"></i>
                    </a> | built using the Yelp API
                </div>

            </div>

        </div>

    </footer>




    <!-- =====================SCRIPTS===================== -->
    <!-- jQuery -->
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <!-- Bootstrap -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <!-- AngularJS -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js"></script>
    <!-- custom scripts -->
    <script src="js/app.js"></script>
    <script src="js/scripts.js"></script>
</body> 

results.html (the template I want to load in on '/' route)

<p>Results are loading</p>
<div class="row result" ng-repeat="result in data">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-8 col-lg-offset-2 result">
        <div class="row">

            <!-- image -->
            <div class="col-xs-3 col-sm-2">
                <div class="image">
                    <img ng-src="{{ result.image || 'img/noimage.jpg' }}">
                </div>
                <div ng-click="addGoing($index)" class="going text-center">
                    {{ result.going || 0 }} going
                </div>
            </div>


            <!-- text -->
            <div class="col-xs-9 col-sm-10">
                <h4><a ng-href="{{ result.url || '#' }}">{{ result.name }}</a> 
                    <small>{{ 'Rating: ' + result.rating || 'No rating available' }}</small></h4>
                <p>{{ result.text || 'No reviews for this establishment.' }}</p>
            </div>

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

As I said, if I remove all the routing logic from app.js and ng-view from index.html and just add in results.html, the app is running fine, controller is loading properly and everything is ok. But upon adding 'ngRoute' to the app, the results template doesn't load and I can't access any mainController variables or logic from index.html.

If anyone knows what I'm doing wrong please let me know. Thanks!

Edit: I just noticed the cdn for angular-route.js is not working. I can't find a download link for this library in the official site and I don't have bower so I have no idea where to get this file.

mlamp
  • 773
  • 2
  • 9
  • 21

2 Answers2

1

There is an error for angular-route.js https://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js

its adress is wrong. There is no version for it. Write your version insteadof X.Y.Z suchas https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-route.js

Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57
1

You need to change X.Y.Z by any specific version like 1.4.8 in your index.html page for angular route.

So you should use

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>

instead of

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js"></script>
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68