Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to connect to socket each tym when a specific ng-view is loaded? and destroy when unload?
    text
    copied!<p>I m trying to build an web app with angular socket.io and nodejs. is it possible to connect to server socket each tym when an specific template is loaded on ng-view? i tried it in several way. it works only the first time. i have to refresh the page when i loaded another view and come back to the old one, otherwise it does not works ! thank u in advance ! server side code:</p> <pre><code>io.on("connection", function(socket){ Events.find().exec(function(err,existing_events){ if(err) { console.log(err); socket.emit('err',{message: "events can not be loaded now!"}) } else { socket.emit('events',{events: existing_events}); } }); } </code></pre> <p>here is client side code: angular configuration:</p> <pre><code>window.app.config(['$routeProvider', '$locationProvider', function($routeProvider,$locationProvider) { $locationProvider.html5Mode(true); $locationProvider.hashPrefix('!'); $routeProvider. when('/event', { templateUrl: '/views/eventsHome.html' }). when('/', { templateUrl: 'views/index.html' }). otherwise({ redirectTo: '/' }); } ]); </code></pre> <p>eventsHome.html:</p> <pre><code>&lt;div ng-controller="eventsCtrl"&gt; &lt;label style="width:15%;float:left;"&gt;Current Events&lt;/label&gt; &lt;a style="float:left;" href="" id="create" ng-click="create()"&gt;Create Event&lt;/a&gt; &lt;div style="border:none; width:90%; max-height:200px; overflow:auto;overflow-x:hidden; clear:both; padding-left:2%; margin-top:5%;"&gt; &lt;div id='events' ng-repeat="event in events"&gt; &lt;a href="/event/{{event.name}}" ng-click="leave()"&gt;{{event.name}}&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>eventsCtrl: </p> <pre><code>angular.module('myApp.system').controller('eventsCtrl', ['$scope', 'Global','$location', function ($scope, Global,$location) { var socket; var sessionId = ''; var serverBaseUrl = document.domain; socket = io.connect(serverBaseUrl); $scope.events; socket.on('connect', function () { sessionId = socket.socket.sessionid; console.log('Connected ' + sessionId); //socket.emit('newUser', {id: sessionId, name: user_name}); }); socket.on('events',function(data){ //updateEvents(data.events); $scope.$apply(function () { // body... $scope.events=data.events; }); }); }]); </code></pre>
 

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