Angular JS不呈现HTML

时间:2016-11-24 10:11:30

标签: javascript html angularjs

我是Angular JS的新手。我写了一个简单的Angular代码,但HTML没有正确呈现,或者表达式没有得到评估。请帮助我缺少什么。我正在使用angular.min.js缩小(1.6)。

<!DOCTYPE html>
<html ng-app>
    <head>
        <script src="js/angular.min.js"></script>
        <script>
            function MyFirstCtrl($scope) {
            var employees = ['Catherine Grant', 'Monica Grant',
               'Christopher Grant', 'Jennifer Grant'];

            $scope.ourEmployees = employees;
            };
        </script> 
    </head>
    <body ng-controller='MyFirstCtrl'>
        <h2>Number of Employees: {{ ourEmployees.length}}</h2>      
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

使用ng-app指令为您的应用设置根目录,并为您的应用创建一个控制器,您可以从中处理$scope

var app = angular.module("ng-app", []);
app.controller("myCtrl", function($scope) {
  var employees = ['Catherine Grant', 'Monica Grant', 'Christopher Grant', 'Jennifer Grant'];
  $scope.ourEmployees = employees;

});
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ng-app">
<div ng-controller="myCtrl">
  <h2>Number of Employees: {{ ourEmployees.length}}</h2>
  </div>
</div>