gpt4 book ai didi

javascript - 数据库数组更新后 ng-repeat 不更新

转载 作者:可可西里 更新时间:2023-11-01 10:42:16 27 4
gpt4 key购买 nike

在我的 Mongo 数据库中创建新项目后,我试图让我的 ng-repeat 更新。根据我的研究,这应该会自动发生。我知道对于每个 View ,都会为该 View 创建一个新的 $scope,并且要在所有 $scope 之间共享数据,您将使用服务,我相信这就是我正在做的……但也许不是。

感谢任何帮助,这是我的代码:

newblog.html

<div class="newblog">
<a ui-sref="blogs"><i class="fa fa-close fa-2x exit"></i></a>
<div class="form">
<form>
<i class="fa fa-pencil-square-o fa-5x"></i><br>

<input class="blogtitle" type="text" ng-model="formData.title" placeholder="Blog Title" required /><br>

<textarea ng-model="formData.body" rows="15" placeholder="Write the body of your blog here." required></textarea><br>

<label for="">by:</label><br>
<input class="blogauthor" type="text" ng-model="formData.author" placeholder="Author Name" required /><br><br>

<button type="submit" ng-click="addPost()">Submit</button>
</form>
</div>

这是用户创建的新博客的表单。

ma​​in.html

<div class="wrapper"> 
<div class="blog-container" ng-repeat="data in blog | orderBy: '-pubdate'" ng-click="readPost(data._id)" ui-sref=".readblog">
<p><i class="fa fa-sticky-note"></i>&nbsp;<b>{{ data.title }}</b></p>
<p>by {{ data.author }}</p>
<p>{{ data.pubdate | date }}</p>
<br>
<div>
<p>{{ data.body }}</p>
</div>
<br><br>
<div class="button-box">
<div id="deletePost" ng-click="deletePost(data._id)"><i class="fa fa-trash-o fa-2x"></i></div>
</div>
</div>

这是带有 ng-repeat 的 View 。

下面是我的 Controller 和服务。

ma​​inCtrl.js

var app = angular.module("blog-app");

app.controller('MainController', ['mainService', '$scope', '$http', '$location', '$stateParams', '$state', function(mainService, $scope, $http, $location, $stateParams, $state) {

$scope.formData = {};

$scope.blog = mainService.blog;

function getBlogs() {
mainService.getPosts().then(function(data) {
$scope.blog = data;
console.log(data, "The blogs.");
});
}

getBlogs();

$scope.addPost = function(data) {
mainService.addPost($scope.formData).then(function(data) {
$scope.formData = {};
$location.path('blogs');
$scope.blog.push(data);
console.log(data, 'Blog created.');
});
getBlogs();
};

}]); //End Controller.

ma​​inService.js

var app = angular.module("blog-app");

app.service('mainService', function($http, $q) {

var blog = [];
var readblog = {};
var formData = {};

this.getPosts = function() {
return $http.get('/api/blogs').then(function(response){
blog = response.data;
return blog;
});
}

this.readPost = function(id) {
return $http.get('/api/blogs/' + id).then(function(response) {
readblog = response.data;
return readblog;
});
};

this.addPost = function(formData) {
return $http.post('/api/blogs', formData).then(function(response) {
blog = response.data;
})
};

});

还有我的server.js...

    //Dependencies.
var express = require('express');
var router = express.Router();
// var sendgrid = require('sendgrid')(process.env.U, process.env.PASSWORD)
var bodyParser = require('body-parser');
var cors = require('cors');
var mongoose = require('mongoose');
var mongojs = require('mongojs');
var http = require('http');
var path = require('path');
var fs = require('fs');
var dotenv = require('dotenv');

var port = 9001;
var mongoUri = 'mongodb://localhost:27017/blog-app';

var app = express();

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());

//Blog Data & Data Storage.
//_________________________________________________________________________________________________________________________


var Schema = mongoose.Schema;

var blogSchema = new Schema({
title : {type: String, min: 8, max: 50, required: true},
body : {type: String, required: true},
author : {type: String, min: 3, max: 40, required: true},
pubdate : {type: Date, default: Date.now}
});

var Blog = mongoose.model('Blog', blogSchema);

//Routes.
//_________________________________________________________________________________________________________________________

// GET BLOGS!
app.get('/api/blogs', function(req, res) {
Blog.find(function (err, blogs) {
if (err)
res.send(err);
res.json(blogs);
});
});

app.post('/api/blogs', function(req, res) {
Blog.create({
title: req.body.title,
body: req.body.body,
author: req.body.author,
date: req.body.date
}, function(err, blog) {
if (err)
res.send(err);
Blog.find(function(err, blogs) {
if (err)
res.send(err);
res.json(blogs);
});
});
});

mongoose.connect(mongoUri);
mongoose.connection.once('open', function() {
console.log("Hey there! We are now connected to MongoDB at: ", mongoUri);
});

app.listen(port, function() {
console.log('Magic! Listening on port: ', port);
});

这两天我一直在努力,非常感谢任何帮助!

最佳答案

一个问题可能是您在检索博客之前没有等待发布新帖子。

尝试将 getBlogs() 调用移动到 addPost 调用的回调中:

$scope.addPost = function(data) {
mainService.addPost($scope.formData).then(function(data) {
$scope.formData = {};
$location.path('blogs');
$scope.blog.push(data);
console.log(data, 'Blog created.');

getBlogs();
});
};

关于javascript - 数据库数组更新后 ng-repeat 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37487570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com