简体   繁体   中英

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

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

I expect to get

Number of employees: 4

But I am instead getting

Number of employees: {{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...

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


See the docs for more information - Understanding Controllers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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