watch on an ng-click

Asked

Viewed 132 times

2

I have the following input:

 <input type="submit" value="Publish" 
 ng-click="submit(editAddNotification)" project-focus-invalid-field>

When I run it, it inserts the change into my database (a PUT).

But I am needing that the moment PUT completes it calls a certain function that is in another controller.

I wanted to know if there is a possibility to use the $watch in ng-click for when it fires call this function I created

scope.enableEdit = function() {
                    showAction([
                        'editNavItem',
                        'addNavItem',
                        'editNavItemContent'
                    ]);
                };

The problem is because when it adds something in the database it reset the input parameters.

  • 1

    It would not be the case of a service where you centralize the process of insertion and notification?

  • @Onosendai good, I really thought about it as I took the project this way and my client has a certain 'rush' I would need a solution with the current structure. But you’re absolutely right I’ll try to put a flag on the service it returns.

  • 1

    I’m sorry. = / Dirty and fast way - Jete Rootscope, use broadcast to announce the update and monitor, in your other control, the event.

  • 1

    Sample - $rootScope.$broadcast('nomeDoEvento'). To monitor the event, use $rootScope.$on('nomeDoEvento', function () {...});

  • It’s really gonna get a little dirty, but I’m gonna try here to keep it going and I’m gonna follow your suggestion with the service thing.

  • 1

    If it works, add your own response demonstrating how you implemented it. I did no more than comment. =)

Show 1 more comment

1 answer

0


After checking several options, I decided to use $broadcast, but as @Onosendai said it would be something very dirty, not to be so dirty so I created a service for the broadcast, this way always when I want to call the $rooscope.$broadcast or the $rootScope.$on I just institute my service and make use of it. You can check the code here, I basically created a pubSub with a pubSub.dispathand a pubSub.addListener understand the first as what will be looking and modified case will warn the second that something happened there.

I won’t post the code here because it would be too long, but whoever is interested in the same here I have a gist that contains the service "broadcast" and who has any improvement or doubt I am available. I thank all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.