Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your service shouldn't interact with the DOM directly as you're trying to do. If I were designing such a system I would:</p> <ol> <li>Have a service which simply takes in messages and either puts them in an array (if you want to support a whole list of messages) or simply remembers the last message (if you only want to support a single message)</li> <li>Have a controller which gets the message service injected to it and binds the message(s) from the service into its scope</li> <li>Then have a bit of HTML (or a custom directive) that iterates the messages using the above controller and displays them</li> </ol> <p>Here's what I mean (plunk here: <a href="http://plnkr.co/Eps1Gy" rel="nofollow">http://plnkr.co/Eps1Gy</a>)</p> <p>index.html:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="bower_components/angular/angular.js"&gt;&lt;/script&gt; &lt;script src="scripts/app.js"&gt;&lt;/script&gt; &lt;script src="scripts/controllers/messages.js"&gt;&lt;/script&gt; &lt;script src="scripts/controllers/click.js"&gt;&lt;/script&gt; &lt;script src="scripts/services/messageService.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body ng-app="messagesApp"&gt; &lt;h1&gt;Messages&lt;/h1&gt; &lt;div ng-controller="MessagesCtrl"&gt; &lt;ul ng-repeat="message in messages"&gt; &lt;li&gt;{{message}}&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;a ng-href="#" ng-controller="ClickCtrl" ng-click="addMessage()" &gt;Add a new message!&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>messageService.js:</p> <pre><code>function MessageService() { this.messages = []; this.add = function(msg) { this.messages.push(msg); } } angular.module('messagesApp') .service('messageService', MessageService); </code></pre> <p>messages.js:</p> <pre><code>angular.module('messagesApp') .controller('MessagesCtrl', function ($scope, messageService) { $scope.messages = messageService.messages; }); </code></pre> <p>click.js:</p> <pre><code>angular.module('messagesApp') .controller('ClickCtrl', function ($scope, messageService) { var count = 0; $scope.addMessage = function() { messageService.add('Test message ' + count++); } }); </code></pre> <p>app.js:</p> <pre><code>angular.module('messagesApp', []) .config(function ($routeProvider) { $routeProvider .otherwise({ redirectTo: '/' }); }); </code></pre> <p>Like I said, you can replace the HTML for handling the messages with a custom directive if you want some complex interaction, but the important thing is that you don't try to muck with the view from your service. The service should interact only with a well-defined model.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload