-1
I’m finishing a messaging app, however, I’m not able to make the messages appear on the screen.
HTML:
<div class="card" ng-repeat="mensagem in mensagens">
<div class="item item-text-wrap">
<h2>{{mensagem.usuario}} / {{mensagem.logradouro}}</h2>
<p>{{mensagem.msg}}</p>
<p>{{mensagem.hora}}</p>
</div>
</div>
controller:
.controller('logradouroCtrl', function ($scope, $http, $window, $stateParams, $ionicScrollDelegate, socket) {
$scope.mensagem = {
msg:""
};
var pegaMsgsLogra = function () {
idCep = $window.localStorage.getItem('idCep');
idUsuario = $window.localStorage.getItem('idUsuario');
var nome = $window.localStorage.getItem('nome');
var usuario = $window.localStorage.getItem('usuario');
var uf = $window.localStorage.getItem('uf');
var cidade = $window.localStorage.getItem('cidade');
var bairro = $window.localStorage.getItem('bairro');
var logradouro = $window.localStorage.getItem('logradouro');
socket.message(function(res){
console.log(res);
$scope.mensagens.push(res);
})
$ionicScrollDelegate.resize();
$ionicScrollDelegate.scrollBottom(true);
}
services.js:
.factory('socket', function ($rootScope) {
var wsUri = "ws://localhost:1234";//ocalhost:1234
var socket = new WebSocket(wsUri);
return {
send: function (data) {
socket.send(JSON.stringify(data));
},
message: function(callback){
socket.onmessage = function(ev) {
var result = JSON.parse(ev.data); //PHP sends Json data
console.log(result);
callback(result);
//ev.data são os dados da mensagem que irão aparece na tela
$rootScope.$apply(function () {
**$rootScope.mensagens.push(result);**
});
};
}
};
});
app js.:
.factory('socket', function ($rootScope) {
var wsUri = "ws://localhost:1234";
var socket = new WebSocket(wsUri);
return {
send: function (data, callback) {
socket.send(JSON.stringify(data));
var args = arguments;
},
message: function(){
socket.onmessage = function(ev) {
var result = JSON.parse(ev.data); //PHP sends Json data
$rootScope.$apply(function () {
$rootScope.mensagens.push(result);
});
};
}
};
});
Messages appear this way on the screen:
Cannot read Property 'push' of Undefined
and points to that controller line: $Scope.mensagens.push(res);
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero