我已经使用AngularJS定义了以下服务:
myApp.factory('utilitiesService',
function ($filter) {
return {
myFuncA: function (inArg) {
return "XXXX";
},
myFuncB: function (inObj) {
inObj.c = "CCCC";
inObj.d = "dddd";
inObj.newFunc = function() {
return utilitiesService.myFuncA(
this.c,
this.d
);
};
return inObj;
}
};
}
);
从此服务之外,我可以myFuncA()
通过注入utilitiesService
然后简单地进行utilitiesService.myFuncA("blah blah blah");
但是,myFuncB()
以相同的方式调用失败,因为它说不知道utilitiesService
。所以,我怎么能访问myFuncA()
从内部myFuncB()
的inObj.newFunc()
?
请注意,我不能引用utilitiesService
如this
在定义newFunc()
,因为在这种情况下,this
是inObj
。
这是执行此操作的一种方法:
myApp.factory('utilitiesService',
function ($filter) {
service = {};
service.myFuncA = function (inArg) {
return ("XXXX" + inArg);
};
service.myFuncB = function (inObj) {
inObj.c = "CCCC";
inObj.d = "dddd";
inObj.newFunc = function() {
return service.myFuncA(
this.c,
this.d
);
};
return inObj;
};
return service;
}
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句