在angularjs中从$interval调用函数
我想从$interval的超时调用函数在angularjs中从$interval调用函数,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我想从$interval的超时调用函数 angular.module('myApp').factory('myFactory',function($interval){ var stop; return{ start : function() { if(angular.isUndefined(stop)){ stop = $interval(function() {
angular.module('myApp').factory('myFactory',function($interval){
var stop;
return{
start : function() {
if(angular.isUndefined(stop)){
stop = $interval(function() {
function1();
}
, 30000);
}
},
function1 : function() {
console.log('In function1');
function2();
},
function2 : function() {
console.log('In function2');
}
}
});
我得到的错误是
TypeError:undefined不是函数
请让我知道可能的原因以及如何解决相同的问题
谢谢,
ng-R我的建议是将函数的定义移到返回对象之外,请参见下面的演示
var-app=angular.module('app',[]);
角度.module('app').factory('myFactory',函数($interval){
函数start(){
$interval(函数(){
功能1();
}, 3000);
}
函数function1(){
console.log('infunction1');
函数2();
}
函数function2(){
console.log('infunction2');
}
返回{
开始:开始,
功能1:功能1,
功能2:功能2
};
});
应用程序控制器('homeCtrl',函数($scope,myFactory){
myFactory.start();
});代码>
您能解释一下为什么我们需要将函数的定义移到返回对象之外吗?如果我们将其保留在返回对象中,会出现什么问题@西尔维斯特