title: accessing a variable in controller from an outside function (angular js)
body: <p>I have a chat page where messages are displayed through <code>ng-repeat</code> on the array <code>$scope.messages</code></p>
  
  <p>I am subscribed to a backend-as-a-service and use their api for sending instant messages. When a message is received, the <code>onChatMessage</code> is executed.</p>
  
  <p>What I'd like to do is to push any messages received to the the <code>$scope.messages</code> array so that the chat page displays the newly received message. However I don't know how to access the <code>$scope.messages</code> array from my function. Is this possible?</p>
  
  <p>my controller:</p>
  
  <pre><code>.controller('ChatCtrl',function($scope,$stateParams,$ionicScrollDelegate,$q,principal){
  
   $scope.messages = ["test message 1","test message2];
  
  })
  </code></pre>
  
  <p>This function calls when a message is received:</p>
  
  <pre><code>function onChatMessage(senderID,message){
       // senderID, message are predefined by the api of my backend-as-a-service
       // I'd like to push the message received here to scope.messages 
       // but accessing $scope here leads to undefined error. 
  }
  </code></pre>