Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.4
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.9
Node Version: v6.2.0
Xcode version: Not installed
When i try to use in my application i dont see the provider MyService.
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the MyData provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MyService {
constructor(public http: Http) {
console.log('Hello MyService Provider');
}
getAllt(){
return "teste meu";
}
}
hmm the issue is not in the code you paste here. I’ve tested and it works fine. When you inject myService to the ContactPage you should see the message ‘Hello MyService Provider’ on dev console
already gave you the answer as far as I can tell. You should still import MyService in your page to let it know it’s being used. Only putting it inside your constructor isn’t going to work. Whether you put it inside your declarations or not. I don’t see why you shouldn’t import it besides convenience. There aren’t any downsides to it. You actually already have it globally imported (inside app.module.ts). This spares you the effort to put it in the providers of your component every time.
Read up on Angular Dependency Injection. Oversimplification: you need to import on every page, but if you declare the provide in app.modue.ts, it will be the same provider everywhere, not multiple independent copies.
There are reasons DI is a good thing for browser programming, but it does require a shift in perspective. You might be used to a programming style where different pieces of the program know a lot about the program. Part of the goal here is to ensure that each page needs to know as little as possible to do its job.
Yes, but i declare only in app.modules.ts but my page.ts do not recognize the provider method. Only if i import in this page too. Then the app “visualize” the methods this provider.
TypeScript consists of a bunch of really clever people bolting features onto a language that was thrown together in a week and a half to give Netscape’s marketing department a club with which to try to bash Sun over the head with.
As such, all of the OOP constructs are really just smoke and handwaving using what primitives exist.
When you write:
constructor(private fooService: FooService) {
}
…a lot of stuff happens behind the scenes. Angular’s DI mechanism analyzes what you wrote and knows that it has to provide you a parameter of type FooService. On its face, FooService is just an identifier. Only when you provide a declaration for it (via “import”) does it have a clue what that type is. In fact, under the hood, what it is is a constructor function that instantiates an object with the properties and prototype voodoo that constitutes JavaScript “classes”.
The FooService constructor you are requesting here must match the FooService constructor provided by the code that defines the class, because it is used as a key in a lookup dictionary inside the DI system. This can be the source of many wondrous and mysterious bugs when you unintentially end up with multiple versions of the same class inside your app and DI gives different objects to different consumers. To make matters more exciting, the ensuing error messages are very cryptic and give absolutely no clue as to what is happening (see here).
Hey, off topic but I’m curious about your code example. I’ve made my DI parameters public, private, and with no explicit scope, to see which is better, and I haven’t found a significant difference between public and private. Do you think I should care about this?