gpt4 book ai didi

javascript - AngularJS ng-controller 声明

转载 作者:行者123 更新时间:2023-11-28 07:18:21 25 4
gpt4 key购买 nike

我有以下标记...

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

我希望得到

Number of employees: 4

但我反而得到

Number of employees: {{ourEmployees.length}}

可能出了什么问题?

最佳答案

您声明 Controller 的方式似乎不对。请尝试以下操作,在本例中,'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

关于javascript - AngularJS ng-controller 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com