Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular JS TypeError:f不是函数_Javascript_Angularjs - Fatal编程技术网

Javascript Angular JS TypeError:f不是函数

Javascript Angular JS TypeError:f不是函数,javascript,angularjs,Javascript,Angularjs,我试图弄清楚为什么超时函数会给出错误,从而限制模型值的更改 angularExample.html 错误快照: 您需要先定义callAtTimeout,然后再使用它 var callAtTimeout=function(){console.log("hi")} $timeout(callAtTimeout,3000); Javascript中的初始化不是您需要定义callAtTimeout然后使用它。定义函数,如var callAtTimeout=function(){…}发生在运行时,而不是

我试图弄清楚为什么超时函数会给出错误,从而限制模型值的更改

angularExample.html

错误快照:

您需要先定义
callAtTimeout
,然后再使用它

var callAtTimeout=function(){console.log("hi")}
$timeout(callAtTimeout,3000);

Javascript中的初始化不是

您需要定义
callAtTimeout
然后使用它。

定义函数,如
var callAtTimeout=function(){…}
发生在运行时,而不是编译时(而定义函数,如
function callAtTimeout(){…}
处于编译时)

var callAtTimeout=function(){console.log("hi")}
$timeout(callAtTimeout,3000);
因此,
callAtTimeout
尚未在以下行中定义:

$timeout(callAtTimeout,3000);

callAtTimeout
的声明移到该行上方,或者将其更改为
function callAtTimeout(){…}

您只需重新排列代码的顺序,callAtTimeout函数的定义应该在您使用它之前。工作示例:

(函数(){
var app=angular.module('Tutorial',[]);
app.controller(“MyController”,函数($scope,$timeout){
var callAtTimeout=function(){$scope.data=“hello”;}
$scope.data=“hi”;
$timeout(callAtTimeout,3000);
});
})();

您在调用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);


 }); })();

当两个或多个依赖项放错位置时,也会发生此错误。我知道答案有点离题,但读这篇文章可能会有帮助

    (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
}
})();

我花了很多时间试图修复它。这是我的内部错误。 你只需要

断开角度CLI(Ctrl+c)并

再次运行(ng发球


然后,它开始识别我新定义的所有函数。

该函数被定义为匿名函数,但在到达
var callAtTimeout=function(){…}
行之前,它没有分配给变量
callAtTimeout
。如果定义命名函数,它将在作用域(
function myName(){…}
)的第一行可用。变量也是如此:
var a=b+1;var b=0
变量
a
b
都在作用域的顶部定义,但是当
a
被分配时
b
仍然未定义,
a
将因此计算为
a=undefined+1
,这将导致
NaN
未定义$timeout
    (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
}
})();