For example, in the following code I would like func2 to call func1.
例如,在下面的代码中,我希望func2调用func1。
App.factory("MyLib", function() {
return {
func1: function() {
console.log("func1");
},
func2: function() {
func1();
}
}
});
How can I do this?
我怎样才能做到这一点?
2 个解决方案
#1
2
The safest way is to use the revealing module pattern:
最安全的方法是使用揭示模块模式:
App.factory("MyLib", function() {
var func1 = function() {
console.log("func1");
};
var func2 = function() {
func1();
};
return {
func1: func1,
func2: func2
};
});
#2
0
I usually assign it to a variable name same as factory for easier reading and useful to reuse it when you have nested function as show in the example below.
我通常将它分配给与工厂相同的变量名称以便于阅读,并且当您具有嵌套函数时可以重复使用它,如下例所示。
App.factory("MyLib", function() {
var MyLib = {
func1: function() {
console.log("func1");
},
func2: function() {
MyLib.func1();
angular.forEach(["1","2"], function(value, key) {
//easier to read and understand
MyLib.func1();
},[]);
}
}
return MyLib;
});
#1
2
The safest way is to use the revealing module pattern:
最安全的方法是使用揭示模块模式:
App.factory("MyLib", function() {
var func1 = function() {
console.log("func1");
};
var func2 = function() {
func1();
};
return {
func1: func1,
func2: func2
};
});
#2
0
I usually assign it to a variable name same as factory for easier reading and useful to reuse it when you have nested function as show in the example below.
我通常将它分配给与工厂相同的变量名称以便于阅读,并且当您具有嵌套函数时可以重复使用它,如下例所示。
App.factory("MyLib", function() {
var MyLib = {
func1: function() {
console.log("func1");
},
func2: function() {
MyLib.func1();
angular.forEach(["1","2"], function(value, key) {
//easier to read and understand
MyLib.func1();
},[]);
}
}
return MyLib;
});