AngularJS ng-controller语句

时间:2015-06-01 21:01:59

标签: javascript html angularjs

我有以下标记......

<!DOCTYPE html>
<html lang="en-US" ng-app>
    <head>
        <title>priklad00007</title>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
        <script src="priklad00007.js"></script>
    </head>
    <body ng-controller="MyFirstCtrl">
        <h2>Number of employees: {{ourEmployees.length}}</h2>
    </body>
</html>

使用以下JavaScript ...

function MyFirstCtrl($scope) {
    var employees = ["Karel Novak", "Martin Cervenka", "Jana Zelena", "Frantisek Vomacka"];
    $scope.ourEmployees = employees;
}

我希望得到

  

员工人数:4

但我反而得到

  

员工人数:{{ourEmployees.length}}

可能出现什么问题?

1 个答案:

答案 0 :(得分:0)

你宣布控制器的方式似乎已经过时了。请尝试以下操作,在这种情况下,'app'是您的AngularJS应用程序名称...

angular.module('app').controller('MyFirstCtrl', ['$scope', function($scope) {
    var employees = ["Karel Novak", "Martin Cervenka", "Jana Zelena", "Frantisek Vomacka"];
    $scope.ourEmployees = employees;    
}]);

JSFiddle Link - 工作示例

有关详细信息,请参阅文档 - Understanding Controllers