0

I have to make something like global function, but im making mistake somewhere, could anyone see and help me fix this:

 <div>
 <p class="comment-name">{{getName}}</p>
            <p class="comment-mail">{{getMail}}</p>
        <div class="comment_content">
            <p class="comment-com">{{getComm}}</p>
            <button class="post-comm" ng-click="postCom()">Post</button>
 
    </div>

AngularJS:

myApp.run(function ($rootScope) {
     $rootScope.postCom = function () {
         $rootScope.getName = $rootScope.name;
         $rootScope.getMail = $rootScope.mail;
         $rootScope.getComm = $rootScope.comment;
     };

 });
  • help me fix what? What do you expect to happen and what happens instead? Describe what the problem is if you want us to help you fx it. – JB Nizet Jun 16 '18 at 22:26
  • I have made div for comments and when i type my comment in input he displays on webpage but when i refresh page he dissapear. I want to make him global, on that way he will stay on page (i did that with pure JS and it worked, but im new with angularJS and im not sure how to do that) – Aleksandar Jun 16 '18 at 22:29
  • See [AngularJS FAQ - `$rootScope` exists, but it can be used for evil](https://docs.angularjs.org/misc/faq#-rootscope-exists-but-it-can-be-used-for-evil). If you're tempted to put a function on `$rootScope`, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested. – georgeawg Jun 16 '18 at 22:29
  • Georgeawg do you have better idea or way how to do that ? – Aleksandar Jun 16 '18 at 22:30
  • I dont know how to do it with service :/ – Aleksandar Jun 16 '18 at 22:33
  • If you refresh the page, whether you use AngularJS or not, all you have stored in JavaScript variables, global or not, is lost. If you want data to survive a refresh, it needs to be stored on a persistent location: the server, or local storage, or a cookie, for example. You also didn't answer the question: the code you posted has othing to do with your comments story, and you haven't told us what you expected this code to do and what it did instead. – JB Nizet Jun 16 '18 at 22:34
  • Okay, i have routed book (ng-route) and when i type in this inputs my comment and press button (post) i get that comment displayed on my screen, but when i go back and click again on book, that comment is gone (not refresh, only back on other routed page) i hope you understand what im talking about – Aleksandar Jun 16 '18 at 22:37
  • When you switch views with a router, the controller associated with the view is destroyed. If you wish data to persist between views, use a service. Services are singletons which last for the entire lifetime of the app. See [AngularJS Developer Reference - Services](https://docs.angularjs.org/guide/services). – georgeawg Jun 16 '18 at 22:43
  • ahh i see, thank you very much :) thats what i needed but when i searched for some kind of "global function" i always got $rootScope – Aleksandar Jun 16 '18 at 22:45
  • I disagree with the solution of using a service to store the comments. That won't work if you refresh. That won't work if someone else edits or removes the comments. You should get the book information and its associated comments **from the server**, every time you need to display them. – JB Nizet Jun 16 '18 at 22:50
  • OP states "not refresh, only back on other routed page". – georgeawg Jun 16 '18 at 22:52

0 Answers0