Can I listen on a change in service?

13 views
Skip to first unread message

Yonatan Kra

unread,
Feb 6, 2014, 12:11:40 PM2/6/14
to ang...@googlegroups.com
Hi,
I have a service that keeps my data across views (a simple json).
Is there a way for me to watch on changes for this service from a directive?

Chris Rhoden

unread,
Feb 6, 2014, 12:21:34 PM2/6/14
to ang...@googlegroups.com
You could bind to the service from your directive. 

mod.directive('myDirective', ['myService', function (myService) {
  return {
    restrict: 'A',
    ...
  };
});

This is usually not necessary, though, and probably means that the directive is doing too much if it's absolutely necessary.

ng-model could, for instance, depend on a Models service to pull its object, but it uses attributes instead. Generally, that's the right way to go.


--
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/groups/opt_out.



--
chrisrhoden

Yonatan Kra

unread,
Feb 6, 2014, 1:37:19 PM2/6/14
to ang...@googlegroups.com

Hi. Thanks. I'm an almost total newbie... could you explain please (or refer me to an example) how to use ng-model? I tried to use it in mt partial but it kept ignoring the service value.

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/anQ2IGFnd4o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Chris Rhoden

unread,
Feb 6, 2014, 1:42:00 PM2/6/14
to ang...@googlegroups.com
<script>function MyCtrl($scope, myService) { $scope.service = myService; }</script>
<div ng-controller="MyCtrl">
   <input ng-model="service.value" />
</div>

If this doesn't help, please link to a plunkr that isn't working but you think should be, and we might be able to fix it for you.
Reply all
Reply to author
Forward
0 new messages