我是MEAN的前身,我有一个MEAN堆栈应用程序,它将数据拉到一个HTML页面上的一个表中,但现在我想将数据从两个mongo db或集合中提取到同一页面上的另一个表中但不确定最好的方法是什么。我需要使用两个ng控制器吗?任何帮助非常感谢。
HTML:
<div class="risklog" ng-controller="AppCtrl">
<table class="logs">
<thead class="logheaders">
<tr>
<th>RiskID</th>
<th>Description</th>
<th>Probability</th>
<th>Impact</th>
<th>Actions</th>
<th>Owner</th>
<th>RiskLevel</th>
<th>Action</th>
<th> </th>
</tr>
</thead class="logheaders">
<tbody>
<tr>
<td><input ng-model="risk.RiskID"></td>
<td><input ng-model="risk.Description"></td>
<td><select ng-model="risk.Probability">
<option value="High">High</option>
<option value="Medium">Medium</option>
<option value="Low">Low</option>
</td>
<td><select ng-model="risk.Impact">
<option value="High">High</option>
<option value="Medium">Medium</option>
<option value="Low">Low</option>
</td>
<td><input class="form-control" ng-model="risk.Actions"></td>
<td><input class="form-control" ng-model="risk.Owner"></td>
<td><select class="form-control" ng-model="risk.RiskLevel">
<option value="High">High</option>
<option value="Medium">Medium</option>
<option value="Low">Low</option>
</td>
<td><button class="btn-blue" ng-click="addRisk()">Add Risk</button></td>
<td><button class="btn-blue" ng-click="update()">Update</button> <button class="btn-blue" ng-click="deselect()"> Clear </button></td>
</tr>
<tr ng-repeat="risk in risklog">
<td>{{risk.RiskID}}</td>
<pre><td>{{risk.Description}}</td></pre>
<td>{{risk.Probability}}</td>
<td>{{risk.Impact}}</td>
<td>{{risk.Actions}}</td>
<td>{{risk.Owner}}</td>
<td>{{risk.RiskLevel}}</td>
<td><button class="btn-red" ng-click="remove(risk._id)">Remove</button></td>
<td><button class="btn-orange" ng-click="edit(risk._id)">Edit</button></td>
</tr>
</tbody>
</table>
JS控制器
ar myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/contactlist').success(function(response) {
console.log("I got the data I requested");
$scope.risklog = response;
$scope.risk = "";
});
};
refresh();
$scope.addRisk = function() {
console.log($scope.risk);
$http.post('/contactlist', $scope.risk).success(function(response) {
console.log(response);
refresh();
});
};
$scope.remove = function(id) {
console.log(id);
$http.delete('/contactlist/' + id).success(function(response) {
refresh();
});
};
$scope.edit = function(id) {
console.log(id);
$http.get('/contactlist/' + id).success(function(response) {
$scope.risk = response;
});
};
$scope.update = function() {
console.log($scope.risk._id);
$http.put('/contactlist/' + $scope.risk._id, $scope.risk).success(function(response) {
refresh();
})
};
$scope.deselect = function() {
$scope.risk = "";
}
}]);
服务器JS
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('risklog', ['risklog']);
var bodyParser = require('body-parser');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.get('/contactlist', function (req, res) {
console.log('I received a GET request');
db.risklog.find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.post('/contactlist', function (req, res) {
console.log(req.body);
db.risklog.insert(req.body, function(err, doc) {
res.json(doc);
});
});
app.delete('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.risklog.remove({_id: mongojs.ObjectId(id)}, function (err, doc) {
res.json(doc);
});
});
app.get('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.risklog.findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
res.json(doc);
});
});
app.put('/contactlist/:id', function (req, res) {
var id = req.params.id;
console.log(req.body.name);
db.risklog.findAndModify({
query: {_id: mongojs.ObjectId(id)},
update: {$set: {RiskID: req.body.RiskID, Description: req.body.Description, Probability: req.body.Probability, Impact: req.body.Impact, Actions: req.body.Actions, Owner: req.body.Owner, RiskLevel: req.body.RiskLevel}},
new: true}, function (err, doc) {
res.json(doc);
}
);
});
app.listen(3000);
console.log("Server running on port 3000");
答案 0 :(得分:2)
以同样的方式对一组数据进行操作
var refresh = function () {
$http.get('/contactlist').success(function (response) {
console.log("I got the data I requested");
$scope.risklog = response;
$scope.risk = "";
});
};
var loadSomethingElse = function () {
$http.get('/somethingElse').success(function (response) {
console.log("I got the something else I requested");
$scope.somethingElseOnScope = response;
});
};
然后在html中
<tr ng-repeat="something in somethingElseOnScope">
<td>{{something.name}}</td>
</tr>