Again, you should not do this. This is not the angular way.
var $injector = angular.element(document.querySelector('[ng-app]')).injector
();
var $http = $injector.get('myService');
Working Example:
var app = angular.module('plunker', []);
app.service('myService', [function (){
this.test = function (){
alert("Hello");
}
}]);
//Let Angular do it's job first
setTimeout(function(){
var $injector =
angular.element(document.querySelector('[ng-app]')).injector();
var myService = $injector.get('myService');
myService.test()
}, 0);
On Monday, May 4, 2015 at 2:31:39 PM UTC+3, Gaurav Ashara wrote:
>
> Thanks for your reply.
>
> But i have define my service and i want to access its method in javascript
> .
> for example
>
> *app.js *
>
> var app = angular.module('app', []);
>
> app .service('myservice', ['$log',function ($log){
> this.test = function ()
> {
> alert("Hello");
> }
>
> };
>
> *serviceAcess.js *
>
> myservice.test();
>
>
> So, How can i access my services method in my javascript?
>
>
> Thanks & Regards
> Gaurav Ashara
>
> On Monday, May 4, 2015 at 1:34:44 PM UTC+5:30, Bogdan Alexe wrote:
>>
>> I can not see a reason for doing this but it can be achieved trough
>> getting hold on main injector. You can query the DOM for (a node inside)
>> angular root element (the one with ng-app attribute) and wrapping it in an
>> angular.element(), then you can call the injector() method.
>>
>> Example:
>> var $injector = angular.element(document.querySelector('[ng-app]')).
>> injector();
>> var $http = $injector.get('$http');
>>
>>
>>
>>
>> On Sunday, May 3, 2015 at 11:41:37 AM UTC+3, Gaurav Ashara wrote:
>>>
>>> Hello All,
>>>
>>> I want to access my service object in plain javascript class.
>>>
>>> Any buddy did please help me for that.
>>>
>>> Thanks & Regards
>>> Gaurav Ashara
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.