title: How to use compile function in AngularJS directive to ng-repeat elements from a service
body: <p>Could someone, please, show me what I need to do to render in a directive new elements stored in an array in a service. In the example below, an alert from the service shows that each new element is added to the elements array, but how to make the directive show these new elements on the page?</p>
  
  <p>I tried to read everything about <code>compile</code> function in a directive, but could not understand how to make my example work.</p>
  
  <p><strong>Here is a <a href="http://jsfiddle.net/Vczj6/11/" rel="nofollow">jsfiddle</a>.</strong> All I need is to render new messages inside the directive after they are added to the message array in the service. </p>
  
  <p>The <code>Chat</code> service is injected into directive as a <code>chat</code> variable, and in the directive template I want to repeat every message from the service:</p>
  
  <pre><code>'&lt;ul&gt;' +
       '&lt;li ng-repeat="message in chat.messages"&gt;' +
            '&lt;strong&gt;{{message.name}}&lt;/strong&gt; {{message.text}}' +
        '&lt;/li&gt;' +
  '&lt;/ul&gt;'
  </code></pre>
  
  <p>Sample code is on <a href="http://jsfiddle.net/Vczj6/11/" rel="nofollow">jsfiddle</a> and below:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div ng-app="myApp"&gt;
  &lt;my-simple-chat title="AngularJS Chat"&gt;&lt;/my-simple-chat&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>angular.module('myApp', [])
  .factory('Chat', function () {
      var messages = [];
      function sendMessage(name, text) {
          messages.push(
              {
                  name: name,
                  text: text
              });
          alert("Sending message from factory: " + name + ", " + text + " : " + messages.length);       
      };
  
      return {
          messages: messages,
          sendMessage: sendMessage
      };
  
  })
  .directive('mySimpleChat', ['Chat', function (chat) {
  
      var tmpl = '&lt;div&gt;&lt;h2&gt;{{title}}&lt;/h2&gt;' +
                      '&lt;input type="text" ng-model="name" placeholder="Type your name"/&gt;' +
                      '&lt;hr /&gt;' +
                      '&lt;form ng-submit="sendMessage()"&gt;' +
                          '&lt;input type="text" ng-model="text" required placeholder="Type a new message..."/&gt;' +
                          '&lt;input type="submit" id="submit" value="Send"/&gt;' +
                      '&lt;/form&gt;' +
                          '&lt;ul&gt;' +
                              '&lt;li ng-repeat="message in chat.messages"&gt;' +
                                  '&lt;strong&gt;{{message.name}}&lt;/strong&gt; {{message.text}}' +
                              '&lt;/li&gt;' +
                          '&lt;/ul&gt;' +
                  '&lt;div&gt;';
  
      return {
          restrict: 'E',
          replace: true,
          scope: { title: '@title' },
          template: tmpl,
  
          controller: ['$scope', '$element', '$attrs', '$transclude',
            function ($scope, $element, $attrs, $transclude) {
                $scope.name = 'MyName';
                $scope.text = '';
  
                $scope.sendMessage = function () {
                    chat.sendMessage($scope.name, $scope.text);
                    $scope.text = '';
  
                };
  
            }],
  
      };
  }]);
  </code></pre>