在 AngularJs 中,如果调用服务方法:
app.service('nameService', function(){
this.Service = function (){console.log('hello')}
}
然后你就可以使用这个服务(对象): nameService.Service() 所以我的问题是如何编写具有两个参数(名称,函数)的函数
function service ( name, function ){
// how angular declare object with that value of first argument ?
}
我问如何编写像 service(name,fn){} 这样的函数?
最佳答案
AngularJs 使用 Dependency Injection注入(inject)服务。
“service”方法可用于 Angular 对象,因为它位于其原型(prototype)上。
angular.prototype.service = ...
您可以阅读有关原型(prototype)继承的更多信息,MDN - Inheritance and the prototype chain
关于javascript 如何创建函数服务(构造函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39368874/