Javascript Angular JS TypeError:f 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30620895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS TypeError: f is not a function
提问by Ajay
I'm trying to figure out as to why my timeout function is giving error, thereby restricting the change in model value.
我试图弄清楚为什么我的超时函数会出错,从而限制了模型值的变化。
angularExample.html
angularExample.html
<!DOCTYPE html>
<html ng-app="Tutorial">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="scripts/app.js"></script>
</head>
<body ng-controller="MyController">
<input type="text" ng-model="data" />
</body>
</html>
app.js
应用程序.js
(function() {
var app = angular.module('Tutorial', []);
app.controller("MyController",function($scope,$timeout){
$scope.data="hi";
$timeout(callAtTimeout,3000);
var callAtTimeout=function(){$scope.data="hello";}
});
})();
Error Snapshot:
错误快照:
采纳答案by Zee
回答by Donal
You just need to re-arrange the order of your code, the definition for callAtTimeout function should be before you use it. Working example:
你只需要重新安排你的代码的顺序,callAtTimeout 函数的定义应该在你使用它之前。工作示例:
(function() {
var app = angular.module('Tutorial', []);
app.controller("MyController",function($scope,$timeout){
var callAtTimeout=function(){$scope.data="hello";}
$scope.data="hi";
$timeout(callAtTimeout,3000);
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Tutorial" ng-controller="MyController">
<input type="text" ng-model="data" />
</body>
回答by V31
You are defining the callAtTimeout function after it its call. You need to have it above it.
您在调用之后定义 callAtTimeout 函数。你需要把它放在上面。
Sample code:
示例代码:
(function () {
var app = angular.module('Tutorial', []);
app.controller("MyController", function ($scope, $timeout) {
var callAtTimeout = function () {
$scope.data = "hello";
}
$scope.data = "hi";
$timeout(callAtTimeout, 3000);
}); })();
回答by antares667367
The error can also occur when two or more dependencies are misplaced. I know the answer is kinda off topic but reading this might help someone.
当两个或多个依赖项放错位置时,也会发生该错误。我知道答案有点偏离主题,但阅读本文可能会对某人有所帮助。
(function() {
'use strict';
angular
.module('app')
.controller('myController', myController);
myController.$inject = ['dependency1','dependency2','dependency3'];
/* @ngInject */
function myController(dependency1, dependency3, dependency2){
// will trigger the error because dependency 2 && 3 are misplaced
var v = dependency2.somefunction(arg1,arg2,...);
// sometimes it can be difficult to see at the first look
// especially when you have many dependencies
}
})();
回答by Tom
Defining functions such as var callAtTimeout = function() { ... }happens at run time, not at compile time (whereas defining functions such as function callAtTimeout() { ... }is at compile time).
定义诸如var callAtTimeout = function() { ... }在运行时而不是在编译时发生的函数(而定义诸如function callAtTimeout() { ... }在编译时发生的函数)。
Because of this, callAtTimeoutis not yet defined on the line:
正因为如此,callAtTimeout还没有定义就行了:
$timeout(callAtTimeout,3000);
Either move the declaration of callAtTimeoutabove that line, or change it to function callAtTimeout() { ... }
将声明移动到callAtTimeout该行上方,或将其更改为function callAtTimeout() { ... }
回答by shaheer shukur
I had spend a lot of time trying to fix it. It was an internal error in my case. You just have to
我花了很多时间试图修复它。就我而言,这是一个内部错误。你只需要
break the Angular CLI (Ctrl + c) and
run (ng serve) again.
打破 Angular CLI ( Ctrl + c) 和
再次运行(ng serve)。
It then started recognizing all my newly defined functions.
然后它开始识别我所有新定义的功能。
回答by Oam Psy
You need to define callAtTimeoutthen use it.
您需要定义callAtTimeout然后使用它。

