简体   繁体   English

AngularJS ng-controller语句

[英]AngularJS ng-controller statement

I have the following markup... 我有以下标记...

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

With the following JavaScript... 使用以下JavaScript ...

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

I expect to get 我希望得到

Number of employees: 4 员工人数:4

But I am instead getting 但是我反而越来越

Number of employees: {{ourEmployees.length}} 员工人数:{{ourEmployees.length}}

What could be going wrong? 可能出什么问题了?

The way you are declaring controllers seems to be off. 您声明控制器的方式似乎已关闭。 Try the following, where in this case, 'app' is your AngularJS app name... 请尝试以下操作,在这种情况下, '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 - working example JSFiddle Link-工作示例


See the docs for more information - Understanding Controllers 有关更多信息,请参阅文档- 了解控制器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM