Angularjs binding value from service












1















I wish to share a service value between one or more controllers (only one in the following example but that's not the point).



The problema is that the value hold in the service is not bound and shown in the view.



The code (derived from angularjs basic service example) is:



(function(angular) {
'use strict';
angular.
module('myServiceModule', ).
controller('MyController', ['$scope', 'notify','$log', function($scope, notify, $log) {
$scope.callNotify = function(msg) {
notify.push(msg);
};

$scope.clickCount = notify.clickCount();
$log.debug("Click count is now", $scope.clickCount);
}]).
factory('notify', ['$window','$log', function(win,$log) {
var msgs = ;
var clickCounter = 0;
return {
clickCount: function() {
clickCounter = msgs.length;
$log.debug("You are clicking, click count is now", clickCounter);
return clickCounter;
},
push: function(msg) {
msgs.push(msg);
clickCounter = msgs.length;
$log.debug("Counter is", clickCounter);
if (msgs.length === 3) {
win.alert(msgs.join('n'));
msgs = ;
}
}
}
}]);


I wish the counter to be displayed on page:



<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-services-usage-production</title>


<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="script.js"></script>



</head>
<body ng-app="myServiceModule">
<div id="simple" ng-controller="MyController as self">
<p>Let's try this simple notify service, injected into the controller...</p>
<input ng-init="message='test'" ng-model="message" >
<button ng-click="callNotify(message);">NOTIFY</button>
<p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
</div>
<div>You have clicked {{clickCount}} times</div>
</body>
</html>


See it in action on plunker



UPDATE: corrected the trivial errors is html and service code as suggested by @SehaxX










share|improve this question





























    1















    I wish to share a service value between one or more controllers (only one in the following example but that's not the point).



    The problema is that the value hold in the service is not bound and shown in the view.



    The code (derived from angularjs basic service example) is:



    (function(angular) {
    'use strict';
    angular.
    module('myServiceModule', ).
    controller('MyController', ['$scope', 'notify','$log', function($scope, notify, $log) {
    $scope.callNotify = function(msg) {
    notify.push(msg);
    };

    $scope.clickCount = notify.clickCount();
    $log.debug("Click count is now", $scope.clickCount);
    }]).
    factory('notify', ['$window','$log', function(win,$log) {
    var msgs = ;
    var clickCounter = 0;
    return {
    clickCount: function() {
    clickCounter = msgs.length;
    $log.debug("You are clicking, click count is now", clickCounter);
    return clickCounter;
    },
    push: function(msg) {
    msgs.push(msg);
    clickCounter = msgs.length;
    $log.debug("Counter is", clickCounter);
    if (msgs.length === 3) {
    win.alert(msgs.join('n'));
    msgs = ;
    }
    }
    }
    }]);


    I wish the counter to be displayed on page:



    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Example - example-services-usage-production</title>


    <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
    <script src="script.js"></script>



    </head>
    <body ng-app="myServiceModule">
    <div id="simple" ng-controller="MyController as self">
    <p>Let's try this simple notify service, injected into the controller...</p>
    <input ng-init="message='test'" ng-model="message" >
    <button ng-click="callNotify(message);">NOTIFY</button>
    <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
    </div>
    <div>You have clicked {{clickCount}} times</div>
    </body>
    </html>


    See it in action on plunker



    UPDATE: corrected the trivial errors is html and service code as suggested by @SehaxX










    share|improve this question



























      1












      1








      1








      I wish to share a service value between one or more controllers (only one in the following example but that's not the point).



      The problema is that the value hold in the service is not bound and shown in the view.



      The code (derived from angularjs basic service example) is:



      (function(angular) {
      'use strict';
      angular.
      module('myServiceModule', ).
      controller('MyController', ['$scope', 'notify','$log', function($scope, notify, $log) {
      $scope.callNotify = function(msg) {
      notify.push(msg);
      };

      $scope.clickCount = notify.clickCount();
      $log.debug("Click count is now", $scope.clickCount);
      }]).
      factory('notify', ['$window','$log', function(win,$log) {
      var msgs = ;
      var clickCounter = 0;
      return {
      clickCount: function() {
      clickCounter = msgs.length;
      $log.debug("You are clicking, click count is now", clickCounter);
      return clickCounter;
      },
      push: function(msg) {
      msgs.push(msg);
      clickCounter = msgs.length;
      $log.debug("Counter is", clickCounter);
      if (msgs.length === 3) {
      win.alert(msgs.join('n'));
      msgs = ;
      }
      }
      }
      }]);


      I wish the counter to be displayed on page:



      <!doctype html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Example - example-services-usage-production</title>


      <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
      <script src="script.js"></script>



      </head>
      <body ng-app="myServiceModule">
      <div id="simple" ng-controller="MyController as self">
      <p>Let's try this simple notify service, injected into the controller...</p>
      <input ng-init="message='test'" ng-model="message" >
      <button ng-click="callNotify(message);">NOTIFY</button>
      <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
      </div>
      <div>You have clicked {{clickCount}} times</div>
      </body>
      </html>


      See it in action on plunker



      UPDATE: corrected the trivial errors is html and service code as suggested by @SehaxX










      share|improve this question
















      I wish to share a service value between one or more controllers (only one in the following example but that's not the point).



      The problema is that the value hold in the service is not bound and shown in the view.



      The code (derived from angularjs basic service example) is:



      (function(angular) {
      'use strict';
      angular.
      module('myServiceModule', ).
      controller('MyController', ['$scope', 'notify','$log', function($scope, notify, $log) {
      $scope.callNotify = function(msg) {
      notify.push(msg);
      };

      $scope.clickCount = notify.clickCount();
      $log.debug("Click count is now", $scope.clickCount);
      }]).
      factory('notify', ['$window','$log', function(win,$log) {
      var msgs = ;
      var clickCounter = 0;
      return {
      clickCount: function() {
      clickCounter = msgs.length;
      $log.debug("You are clicking, click count is now", clickCounter);
      return clickCounter;
      },
      push: function(msg) {
      msgs.push(msg);
      clickCounter = msgs.length;
      $log.debug("Counter is", clickCounter);
      if (msgs.length === 3) {
      win.alert(msgs.join('n'));
      msgs = ;
      }
      }
      }
      }]);


      I wish the counter to be displayed on page:



      <!doctype html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Example - example-services-usage-production</title>


      <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
      <script src="script.js"></script>



      </head>
      <body ng-app="myServiceModule">
      <div id="simple" ng-controller="MyController as self">
      <p>Let's try this simple notify service, injected into the controller...</p>
      <input ng-init="message='test'" ng-model="message" >
      <button ng-click="callNotify(message);">NOTIFY</button>
      <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
      </div>
      <div>You have clicked {{clickCount}} times</div>
      </body>
      </html>


      See it in action on plunker



      UPDATE: corrected the trivial errors is html and service code as suggested by @SehaxX







      angularjs angularjs-scope






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 18:30







      lrkwz

















      asked Nov 12 '18 at 15:18









      lrkwzlrkwz

      3,88222444




      3,88222444
























          1 Answer
          1






          active

          oldest

          votes


















          0














          First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.



          <body ng-app="myServiceModule">
          <div id="simple" ng-controller="MyController">
          <p>Let's try this simple notify service, injected into the controller...</p>
          <input ng-init="message='test'" ng-model="message" >
          <button ng-click="callNotify(message);">NOTIFY</button>
          <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
          <div>You have clicked {{clickCount}} times</div>
          </div>
          </body>


          Also in your service you are missing return:



          clickCount: function() {
          clickCounter = msgs.length;
          $log.debug("You are clicking, click count is now", clickCounter);
          return clickCounter;
          },


          And in your controller you only once call the notify.clickCount() so you need to add it to the method:



          $scope.callNotify = function(msg) {
          notify.push(msg);
          $scope.clickCount = notify.clickCount();
          $log.debug("Click count is now", $scope.clickCount);
          };


          Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.



          Cheers,






          share|improve this answer


























          • Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

            – lrkwz
            Nov 12 '18 at 18:29











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265117%2fangularjs-binding-value-from-service%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.



          <body ng-app="myServiceModule">
          <div id="simple" ng-controller="MyController">
          <p>Let's try this simple notify service, injected into the controller...</p>
          <input ng-init="message='test'" ng-model="message" >
          <button ng-click="callNotify(message);">NOTIFY</button>
          <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
          <div>You have clicked {{clickCount}} times</div>
          </div>
          </body>


          Also in your service you are missing return:



          clickCount: function() {
          clickCounter = msgs.length;
          $log.debug("You are clicking, click count is now", clickCounter);
          return clickCounter;
          },


          And in your controller you only once call the notify.clickCount() so you need to add it to the method:



          $scope.callNotify = function(msg) {
          notify.push(msg);
          $scope.clickCount = notify.clickCount();
          $log.debug("Click count is now", $scope.clickCount);
          };


          Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.



          Cheers,






          share|improve this answer


























          • Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

            – lrkwz
            Nov 12 '18 at 18:29
















          0














          First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.



          <body ng-app="myServiceModule">
          <div id="simple" ng-controller="MyController">
          <p>Let's try this simple notify service, injected into the controller...</p>
          <input ng-init="message='test'" ng-model="message" >
          <button ng-click="callNotify(message);">NOTIFY</button>
          <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
          <div>You have clicked {{clickCount}} times</div>
          </div>
          </body>


          Also in your service you are missing return:



          clickCount: function() {
          clickCounter = msgs.length;
          $log.debug("You are clicking, click count is now", clickCounter);
          return clickCounter;
          },


          And in your controller you only once call the notify.clickCount() so you need to add it to the method:



          $scope.callNotify = function(msg) {
          notify.push(msg);
          $scope.clickCount = notify.clickCount();
          $log.debug("Click count is now", $scope.clickCount);
          };


          Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.



          Cheers,






          share|improve this answer


























          • Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

            – lrkwz
            Nov 12 '18 at 18:29














          0












          0








          0







          First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.



          <body ng-app="myServiceModule">
          <div id="simple" ng-controller="MyController">
          <p>Let's try this simple notify service, injected into the controller...</p>
          <input ng-init="message='test'" ng-model="message" >
          <button ng-click="callNotify(message);">NOTIFY</button>
          <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
          <div>You have clicked {{clickCount}} times</div>
          </div>
          </body>


          Also in your service you are missing return:



          clickCount: function() {
          clickCounter = msgs.length;
          $log.debug("You are clicking, click count is now", clickCounter);
          return clickCounter;
          },


          And in your controller you only once call the notify.clickCount() so you need to add it to the method:



          $scope.callNotify = function(msg) {
          notify.push(msg);
          $scope.clickCount = notify.clickCount();
          $log.debug("Click count is now", $scope.clickCount);
          };


          Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.



          Cheers,






          share|improve this answer















          First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.



          <body ng-app="myServiceModule">
          <div id="simple" ng-controller="MyController">
          <p>Let's try this simple notify service, injected into the controller...</p>
          <input ng-init="message='test'" ng-model="message" >
          <button ng-click="callNotify(message);">NOTIFY</button>
          <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
          <div>You have clicked {{clickCount}} times</div>
          </div>
          </body>


          Also in your service you are missing return:



          clickCount: function() {
          clickCounter = msgs.length;
          $log.debug("You are clicking, click count is now", clickCounter);
          return clickCounter;
          },


          And in your controller you only once call the notify.clickCount() so you need to add it to the method:



          $scope.callNotify = function(msg) {
          notify.push(msg);
          $scope.clickCount = notify.clickCount();
          $log.debug("Click count is now", $scope.clickCount);
          };


          Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.



          Cheers,







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 17:49









          lrkwz

          3,88222444




          3,88222444










          answered Nov 12 '18 at 15:41









          SehaxXSehaxX

          5661516




          5661516













          • Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

            – lrkwz
            Nov 12 '18 at 18:29



















          • Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

            – lrkwz
            Nov 12 '18 at 18:29

















          Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

          – lrkwz
          Nov 12 '18 at 18:29





          Thank you. Please take a look at stackoverflow.com/questions/53268051/… also

          – lrkwz
          Nov 12 '18 at 18:29


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265117%2fangularjs-binding-value-from-service%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Coverage of Google Street View

          Full-time equivalent

          Surfing