2015-06-01 86 views
1

我有以下的標記......AngularJS NG-控制器聲明

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

可能是什麼回事?

+0

必須添加你的函數MyFirstCtrl到你的angular.module –

回答

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