gpt4 book ai didi

javascript - Angular JS Controller 不工作

转载 作者:行者123 更新时间:2023-12-03 11:16:10 24 4
gpt4 key购买 nike

我刚刚开始使用 AngulasJS,当我运行此代码时,我收到错误“参数‘teacherCtrl’不是一个函数,未定义”,但它是明确定义的。请帮忙。

<body>
<script>
function teacherCtrl($scope) {
$scope.teachers = [
{ name: 'Bill Melega', subject: 'History'},
{ name: 'Top Stanfa', subject: 'English'},
{ name: 'Mike Bremer', subject: 'English'}
];
}
</script>
Name: <input type="text" ng-model="search"> {{search}}

<table ng-controller="teacherCtrl">
<tr ng-repeat="teacher in teachers | filter:search">
<td ng-repeat="property in teacher">
{{ property }}
</td>
</tr>
</table>

</body>

最佳答案

如果你已经设置了 ng-app,原因如下:在 Angular 1.3+ 中你不能再像这样定义 Controller 了。你需要整个东西:app.controller(....

It all comes down to how you create controllers. Luckily the fix is the way you should be writing your controllers (though you can opt into the old behavior). When I’ve taught AngularJS in the past, I’ve created controllers using global functions as that makes the story simple. Usually when I’ve taught it before, I’m trying to help people understand the concepts, not the best practices. Starting with AngularJS 1.3, this isn’t the default behavior.

在这里阅读更多相关信息:http://wildermuth.com/2014/11/11/Angular_1_3_and_Breaking_Change_for_Controllers

关于javascript - Angular JS Controller 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337364/

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