javascript - Angular ng-click 负载 Controller

标签 javascript angularjs angularjs-ng-click

我正在尝试练习 Angular ,但我陷入了困境。

如何让ng-click加载displayController?或者我这样做是错误的?

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";
});

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>

最佳答案

您不需要额外的 Controller 。我猜您想显示有关单击的图书的附加信息。

使用引用和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
    });
}]);

和 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>

关于javascript - Angular ng-click 负载 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601755/

相关文章:

javascript - 正文 onunload 事件

javascript - 不要重新分配变量重新加载脚本

angularjs - angular ui modal 不能引用父作用域

javascript - 从 UICalendar 中选择一天

javascript - 使用 ng-click 自动传递 $event?

javascript - 使用 hyperHTML 和 hyperhtml-app 中的链接进行导航

javascript - JQuery 变量没有转移到 JavaScript

javascript - Angular $http.get 总是改变 JSON 中的顺序

javascript - UL 内的 ng-repeat(位于 P 内)不起作用

javascript - 如何在 AngularJs 中将增加的值重置为其初始值