0

I'm trying to practice angular and I'm stuck this this.

How do I make ng-click load the displayController? Or am I doing this wrong way?

The Angular

var bible = angular.module('bible', []);

// Load the Books
bible.controller('listController', ['$scope', '$http', function($scope, $http) {

    $http.get('books.json').success(function(data) {
        $scope.books = data
    });

}]);

bible.controller('displayController', function($scope) {
    $scope.test = "TEST TEXT";
});

The HTML

<div class="row">
        <div class="col-md-12" ng-controller="listController">
            <table class="table">
                <thead>
                    <th>Testament</th>
                    <th>Title</th>
                    <th>Chapters</th>
                </thead>
                <tbody>
                    <tr ng-repeat="book in books">
                        <td>{{book.testament}}</td>
                        <td><a href="#" ng-click="displayController">{{book.title}}</a></td>
                        <td>{{book.chapters}}</td>
                    </tr>
                </tbody>
            </table>

            <div class="display-book" ng-controller="displayController">
                {{test}}
            </div>
        </div>
    </div>
1
  • I guess this isn't the way you are supposed to do it. Whatever is inside the ngClick, should be inside the $scope of the main controller, in your case, listController. What are you trying to achieve with this? Commented Sep 1, 2014 at 8:56

2 Answers 2

3

You don't need the additional controller there. I guess you want to display additional infos about the clicked book.

Use a reference and ngIf

var bible = angular.module('bible', []);

// Load the Books
bible.controller('listController', ['$scope', '$http', function($scope, $http) {
    $scope.selectedBook = null;
    $http.get('books.json').success(function(data) {
        $scope.books = data
    });
}]);

And html:

<div class="row">
    <div class="col-md-12" ng-controller="listController">
         <!-- will be shown, as long as not book is selected -->
        <table data-ng-if="selectedBook == null" class="table">
            <thead>
                <th>Testament</th>
                <th>Title</th>
                <th>Chapters</th>
            </thead>
            <tbody>
                <tr ng-repeat="book in books">
                    <td>{{book.testament}}</td>
                    <td><a href="#" ng-click="selectedBook = book;">{{book.title}}</a></td>
                    <td>{{book.chapters}}</td>
                </tr>
            </tbody>
        </table>
         <!-- will be shown, when a book got selected -->
        <div data-ng-if="selectedBook != null" class="display-book">
           <!-- display the selection table again -->
            <button data-ng-click="selectedBook = null">Back</button>

            {{selectedBook.title}}
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Why do you want to call a separate controller cant you implement the functionality in a separate function like below ,

bible.controller('listController', ['$scope', '$http', function($scope, $http) {

    $http.get('books.json').success(function(data) {
        $scope.books = data
    });

    $scope.display = function(){
         // **YOUR CODE COMES HERE**
    }
}]);

 <td><a href="#" ng-click="display()">{{book.title}}</a></td>

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.