据我了解,Angularjs 服务是通过传入构造函数来创建的:
app.service('serviceName', function(){
this.var1 = 'foo';
this.method1 = function(){
}
});
Angular 使用 new 运算符运行此函数。
我正在使用这项服务,但我没有看到任何“这个”内部人员
https://github.com/shreya5/angular-facebook-utils/blob/master/src/scripts/facebookUser.js
事实上这个函数返回deferred.promise
有人可以解释一下这里发生的事情吗?
最佳答案
您可以自由选择您想要的任何图案。仅仅因为 Angular 使用 new
运行您的函数,并不意味着您必须使用 this
,也不要求您返回默认对象。
例如,这是一个非常好的习惯用法,我在 Angular 服务中经常使用:
function myConstructor(me){
me = me || "Mark";
return {
name: me,
sayit: function(){return "Say my name, " + me}
};
}
var obj = new myConstructor("Steve");
console.log(obj.sayit())
console.log(obj.name)
关于javascript - Angular 服务返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089743/