admin管理员组文章数量:1327286
I have a working angular2 Componen
t.
I implemented a class for some service (using ng.core.Class
if that matters).
What are the minimal steps to inject my service to my Component
? Should I include my service in bootstrap function? Should I use any of ng.core.Inject
or ng.core.Injectable?
All my experiments failed so far.
I have a working angular2 Componen
t.
I implemented a class for some service (using ng.core.Class
if that matters).
What are the minimal steps to inject my service to my Component
? Should I include my service in bootstrap function? Should I use any of ng.core.Inject
or ng.core.Injectable?
All my experiments failed so far.
- I refer to current beta version of angular2 in this question. – Sergey P. aka azure Commented Dec 30, 2015 at 15:16
1 Answer
Reset to default 12You can do it super simple. Just create a class an pass it through providers
property or through bootstrap
For example
// Alternative 1
var Service = ng.core.Class({
constructor : function() {},
someFunction : function() {
console.log('Some function');
}
})
// Alternative 2
var Service = function() {}
Service.prototype.someFunction = function() {
console.log('Some function');
}
Then pass it to the ponent
var Component = ng.core.
Component({
selector: 'cmp',
template : '',
providers : [Service]
}).
Class({
constructor: [Service, function(svc) {
svc.someFunction();
}]
});
Or through bootstrap
ng.platform.browser.bootstrap(Component, [Service]);
Here's an example so you can take a look at it.
Reference
- Class (you can find some examples of its usage in the ments)
本文标签: How to inject custom service to angular component in plain ES5 (Javascript)Stack Overflow
版权声明:本文标题:How to inject custom service to angular component in plain ES5 (Javascript)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742206360a2432887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论