0
所以我想要做的就是創建一個博客文章,並有實時該博客文章更新所有的人觀看的博客。現在我有基本的應用程序設置,讓我列出的所有帖子,創建後,將其保存到蒙戈,並把它實時更新創造職位的人。標準SPA正確!如何發出返回蒙戈查詢與socket.io
所以我想我可以用socket.io實現爲所有的人觀看的實時更新。我已成功地socket.io線了的地方觀看所有的人都在實時,但是從提交的表單中的數據,而不是像創建後的人蒙戈返回的結果被更新的點。
結果更新爲所有的人觀看缺少一些數據,如創建日期是由蒙戈完成後。
這是管理頁面,表格& NG-重複 - 我很高興與此代碼
<div ng-controller="postsController" class="col-sm-8">
<h3>Posts Controller</h3>
<form ng-submit="addPost()">
<label>Title: </label><br /><input type="text" ng-model="post.title" autofocus required></input><br />
<label>By: </label><br /><input type="text" ng-model="post.by" required></input><br />
<label>Body: </label><div text-angular colorpicker ng-model="post.body" ></div><br />
<button type="submit">Post</button>
</form>
<br />
<div ng-repeat="post in posts | orderBy: '-created_at' | limitTo: 1">
<div>
Title: {{ post.title }} <br />
Created: {{ post.created_at | date: 'MMM d, y' }} - By: {{ post.by }} <br /><br />
<div ta-bind ng-model="post.body">{{ post.body }}</div>
<hr />
</div>
</div>
</div>
這是博客頁面
<div ng-repeat="post in posts | filter:search | orderBy: 'created_at'">
<div>
Title: {{ post.title }} <br />
Created: {{ post.created_at | date: 'MMM d, y' }} - By: {{ post.by }} <br /><br />
<div ta-bind ng-model="post.body">{{ post.body }}
</div>
<hr />
</div>
</div>
這是角postsController &插座。 IO聽衆
myApp.controller('postsController', ['$scope', '$location', 'postService', 'socketio', function($scope, $location, postService, socketio) {
$scope.posts = postService.querying();
socketio.on('post', function(msg) {
//var msgPost =
$scope.posts.push(msg);
console.log(msg);
//console.log(msgPost);
});
$scope.addPost = function() {
postService.create($scope.post)
.$promise.then(
function(result) {
$scope.posts = result;
$scope.post = {};
console.log(result);
}
);
};
}]);
這是後期的NodeJS sController & socket.io發射
var Post = require('../models/Posts');
module.exports.create = function (req, res) {
var post = new Post(req.body);
post.save(function (err) {
Post.find({}, function (err, results) {
res.json(results);
io.emit('post', req.body);
});
});
};
我可以清楚地看到,我傳遞的req.body到io.emit但我不能找出我需要把它的位置。我嘗試過幾件事,但都沒有成功。
在此先感謝。
Simon
好了,我想我可能已經解決了嗎?現在就進行測試。更改了$ scope.posts.push(msg);到$ scope.posts = msg;和io.emit('post',req.body);致io.emit('發佈',結果); – ssedwards