我正在使用MEAN堆栈并尝试从对象中提取信息,但是attributs会根据很多因素而改变,因此无法调用它们。有没有办法以其他方式获取信息?这些数据将用于更新monogdb中的不同内容。
关于我的数据库的简单信息:3个集合。用户,年,课程。
年份:id,userid,courses {courseid,grade,}
课程:身份证,姓名,学分等。
我已设法将信息发送到nodejs文件,但无法对其执行任何操作。以下是我所掌握的控制台日志的结果。如何从对象中获取信息以便我可以更新数据库?
日志中的结果
Object {year: Object, grade: Object}
grade:Object
574d8ce6bfb4ea470a2e10c7:"3"
__proto__:Object
year:Object
574e8cf5ae8fc00917ded99b:true
__proto__:Object
Grade contains the id of the course for which I want to change the grade and the grade
I want to change to.
Year contains the Id of the year in which the course is stored.
主/ index.html中
以下仅包含表格。变量year.XXX迭代超过一年(从数据库中检索)。
year.course = {id:XX,年级:YY}
theCourse迭代数据库中的所有课程以获取课程特定信息。
vm.change是发送到控制器的变量。
<form>
<div class="panel-heading " ><b> YEAR {{year.year}}</b>
<label style="float: right;"> Edit
<input type="checkbox" id="year._id" value="year.id" ng-model="vm.change.year[year._id]"/>
</label>
<div><p>Sum of Credits: {{year.totalCredits}}</p></div>
</div>
<div class="panel-body hideyear" >
<div class="col-xs-12">
<h4>HT</h4>
<table class="table " >
<tr class="active" >
<th><b>Course code</b></th>
<th><b>Grade</b></th>
<th><b>Update Grade</b></th>
</tr>
<tbody ng-repeat= "course in year.courses">
<tr ng-repeat="theCourse in vm.courses" ng-if="theCourse._id==course.id && (theCourse.term=='HT1'||theCourse.term=='HT2')">
<td >{{theCourse.courseCode}}</td>
<td>{{course.grade}}</td>
<td ><select ng-disabled="!vm.change.year[year._id]" ng-model="vm.change.grade[course.id]">
<option disabled selected value> -- update grade -- </option>
<option value="NA">NA</option>
<option value="5">5</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="G">G</option>
<option value="U">U</option>
</select></td>
</tr>
</tbody>
</table>
</div>
</div>
<button type="submit" class="btn btn-primary" ng-disabled="!vm.change.year[year._id]" ng-click="vm.saveChange(vm.change)">Save</button>
</div>
</form>
index.controller.js
function saveChange(change){
console.log(change)
}
修改
在一个名为 app.js vm的文件中,作为Controller as并选择要使用的控制器:
(function () {
'use strict';
angular
.module('app', ['ui.router'])
.config(config)
.run(run);
/*
Connects the right subpage with corresponding controller and url.
*/
function config($stateProvider, $urlRouterProvider) {
// default route
$urlRouterProvider.otherwise("/");
$stateProvider
.state('home', {
url: '/',
templateUrl: 'home/index.html',
controller: 'Home.IndexController',
controllerAs: 'vm',
data: { activeTab: 'home' }
});
}
当html加载时,控制器从页面大部分部分使用的数据库中收集一些信息。
function initController() {
// get current user
UserService.GetCurrent().then(function (user) {
vm.user = user;
});
//Gets all years in the database
YearService.GetAll().then(function (years) {
vm.years = years;
});
// Gets all courses in the databased.
CourseService.GetAll().then(function (courses) {
vm.courses = courses;
});
}
上面的initcontroller中的所有内容都完全相同,所以我将展示其中一个的过程,YearSevice.GetAll,调用 app-service / year.service.js
(function () {
'use strict';
angular
.module('app')
.factory('YearService', Service);
function Service($http, $q){
var service = {};
service.GetAll = GetAll;
return service;
function GetAll() {
return $http.get('/api/year/getAll').then(handleSuccess, handleError);
}
使用$ http.get我们通过服务器转到服务器端控制器, year.controller 。
var config = require('config.json');
var express = require('express');
var router = express.Router();
var YearService = require('services/year.service');
// routes
router.get('/getAll', getAll);
function getAll(req,res){
YearService.getAll()
.then(function(result){
if(result){
res.send(result);
}else{
res.sendStatus(404);
}
})
.catch(function(err){
res.status(400).send(err);
});
}
YearService.getAll调用服务器端服务:services / year.services.js
var config = require('config.json');
var _ = require('lodash');
var Q = require('q');
var mongo = require('mongoskin');
//Connectionsstring to the mongo database, if the app is runed on openshift the connectionstring value will change.
var connectionString = "mongodb://localhost:27017/mean-stack-registration-login-example";
if(process.env.OPENSHIFT_MONGODB_DB_URL){
connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + "studycontrol";
}
var db = mongo.db(connectionString, { native_parser: true });
db.bind('years');
db.bind('users');
db.bind('courses');
var service = {};
service.getAll = getAll;
module.exports = service;
function getAll(){
var deferred = Q.defer();
//
db.collection('years').find().toArray(function(err, result) {
if (err) deferred.reject(err);
deferred.resolve(result);
return deferred.promise;
});
return deferred.promise;
}
该集合将一直返回到html代码。向数据库发送新信息具有相同的结构,但包括要在整个时间内发送的对象。在以前的情况下,对象很容易构建,但现在我需要发送课程代码,年份ID和成绩,并尝试使用复选框和选择,所以三个值,但在两个“运营商”。
答案 0 :(得分:1)
要访问存储在您引用的对象中的数据,您可以像这样访问它:
grade.id
或者像这样
grade['id']
如果您不确定对象是否包含任何给定属性,您可以这样检查:
grade.hasOwnProperty('id')
如果您只想登录控制对象的内容,可以这样做:
console.log( JSON.stringify( grade ) );
希望这能回答你的问题
答案 1 :(得分:0)
为什么你这么复杂地做 - ctrl.save(ctrl.courses)
包含所有需要保存的信息。
angular.module('app', [])
.factory('grades', function() {
var grades = {}
grades.courses = [{
code: 'abc',
grade: 1
}, {
code: 'bca',
grade: 5
}, {
code: 'cab',
grade: 3
}]
grades.available = [1, 2, 3, 4, 5]
return grades
})
.controller('firstCtrl', function(grades) {
this.availableGrades = grades.available
this.courses = angular.copy(grades.courses)
this.save = function(courses) {
grades.courses = angular.copy(courses)
// as we have all needed data in controller
// we can also do it like that
//
// grades.courses = angular.copy(this.courses)
}
})
.controller('secondCtrl', function($scope, grades) {
this.availableGrades = grades.available
this.courses = grades.courses
$scope.$watch(function() {
return grades.courses
}, function(courses) {
this.courses = courses
}.bind(this))
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller='firstCtrl as ctrl'>
<ul>
<li ng-repeat='course in ctrl.courses'>
{{ course.code }} -
<select ng-model='course.grade' ng-options='grade for grade in ctrl.availableGrades'></select>
</li>
</ul>
<button ng-click=ctrl.save(ctrl.courses)>Save</button>
</div>
<div ng-controller='secondCtrl as ctrl'>
<ul>
<li ng-repeat='course in ctrl.courses'>
{{ course.code }} -
<select ng-model='course.grade' ng-options='grade for grade in ctrl.availableGrades'></select>
</li>
</ul>
</div>
</div>
&#13;