Problem with directives and services

63 views
Skip to first unread message

Jean Spiteri

unread,
Apr 9, 2015, 10:03:57 AM4/9/15
to ang...@googlegroups.com
Hi all, I am a beginner in Angular.js. I don't know if this is the correct place to post. However, I've been searching for the past days and haven't been able to find a solution for this problem. I have a service which needs to be called from different parts of my app. The code can be found here: https://gist.github.com/Liongold/3158060510cc94ad8e6d. As you can see, the service includes DOM manipulation but from what I've seen this is incorrect and should be done in a directive. Can anyone show me an example or help me with how I can use a directive? I would like to have it that when the service is called, the directive triggers automatically.

Thanks to all those who've read this and sorry for disturbing.

Vytautas Pranksunas

unread,
Apr 9, 2015, 10:19:27 AM4/9/15
to ang...@googlegroups.com
I think AngularJs home site has good documentation on everything: https://docs.angularjs.org/guide/directive
Also there are many examples on the web. 
Also they have little free course for beginners: http://campus.codeschool.com/courses/shaping-up-with-angular-js/intro

Sander Elias

unread,
Apr 9, 2015, 11:01:04 AM4/9/15
to ang...@googlegroups.com
Hi Jean,

Create a directive that creates the html and the holder. This is a simple template, that starts of hidden. Inject your service in that directive, and register a callback into your service.
your notification service can then be injected where you need it. Upon invocation it runs the callback from the directive. 

Is that enough for you?
Regards
Sander

Rafael Bernard Rodrigues Araujo

unread,
Apr 9, 2015, 11:20:50 AM4/9/15
to ang...@googlegroups.com
Hi, Jean.

Sorry, but I can't look into your demo, but I have an application with this behavior and I think you can try to step-by-step improving your knowledge and code: first, you can share your service through your controllers using the dependence injection. Example:

===
app.controller('OneController', ['$scope', 'myService', function($scope, myService) {
   $scope.testing = function () {
    myService.myCall();
  };
}]);

app.controller('TwoController', ['$scope', 'myService', function($scope, myService) {
   $scope.secondTesting = function () {
    myService.myCall();
  };
}]);

app.service('myService', function($http){
  this.myCall = function () {
    alert('OK');
  }
});

===

And you don't need to manipulate the DOM. Inside your controllers you can change the app behavior using scope variables.

Take care,


--
Rafael Bernard Rodrigues Araújo
about.me/rafaelbernard
Analista de Tecnologia | Hospedagem de sites - http://oservidor.com

On Thu, Apr 9, 2015 at 11:03 AM, Jean Spiteri <beimagina...@gmail.com> wrote:
Hi all, I am a beginner in Angular.js. I don't know if this is the correct place to post. However, I've been searching for the past days and haven't been able to find a solution for this problem. I have a service which needs to be called from different parts of my app. The code can be found here: https://gist.github.com/Liongold/3158060510cc94ad8e6d. As you can see, the service includes DOM manipulation but from what I've seen this is incorrect and should be done in a directive. Can anyone show me an example or help me with how I can use a directive? I would like to have it that when the service is called, the directive triggers automatically.

Thanks to all those who've read this and sorry for disturbing.

--
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 angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Jean Spiteri

unread,
Apr 16, 2015, 11:20:02 AM4/16/15
to ang...@googlegroups.com
Thanks to all of you. 
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/6McwSHg_Rn4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.


--
Sincerely,
Jean Spiteri.

Reply all
Reply to author
Forward
0 new messages