javascript - 用范围变量替换 ng-init 不起作用

标签 javascript angularjs

我尝试在下面的 AngularJS 代码中使用范围而不是 ng-init,但没有显示为什么?

   <!doctype html>
    <html ng-app="myApp" ng-controller="myCtrl">
    <head>
    <title>Bookshop - Your Online Bookshop</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
    </head>
    <body>

    <div class="container">
            <h2>Your Online Bookshop</h2>
            <ul class="unstyled">
                    <li ng-repeat="book in books">
                    {{book}}
                    </li>
    </ul>
    </div>

    <div class="container">
            <h2>Your Online Bookshop</h2>
            <ul class="unstyled">
                    <li ng-repeat="book in books_author">
                            <span>{{book.name}} written by {{book.author}}</span>
                    </li>
            </ul>
    </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
    $scope.books = ['Effective Java','Year without Pants','Confessions of public speaker','JavaScript Good Parts'];
    $scope.books_author = [{'name': 'Effective Java', 'author':'Joshua Bloch'},{'name': 'Year without Pants', 'author':'Scott Berkun'},{ 'name':'Confessions of public speaker','author':'Scott Berkun'},{'name':'JavaScript Good Parts','author':'Douglas Crockford'}];
    });
    </script>    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    </body>
    </html>

最佳答案

它对我有用,但你必须在使用它之前加载 Angular 。所以改为:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
 <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
    $scope.books = ['Effective Java','Year without Pants','Confessions of public speaker','JavaScript Good Parts'];
    $scope.books_author = [{'name': 'Effective Java', 'author':'Joshua Bloch'},{'name': 'Year without Pants', 'author':'Scott Berkun'},{ 'name':'Confessions of public speaker','author':'Scott Berkun'},{'name':'JavaScript Good Parts','author':'Douglas Crockford'}];
    });
    </script>    

关于javascript - 用范围变量替换 ng-init 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558885/

相关文章:

javascript - JS 工具提示以错误的方式工作

javascript - JQuery 验证仅适用于提交表单中的第一个元素。 [打算验证所有]

javascript - 检测指令中的 ng-repeat 元素长度变化

angularjs - 使用 Angular Google Maps指令显示标记标签

AngularJS、ui.router、基于用户角色的加载模板和 Controller

javascript - JS 按 Enter 时收到不需要的 'enter/return'

javascript - 资源被解释为样式表但在 AngularJS 中以 MIME 类型 text/html 传输

javascript - 无法让 Fullcalendar 在 cordova 应用程序中显示

javascript - 自动向下滚动 angularjs

javascript - for 循环中的 Angular Promise