我正在使用angular,nodejs,sequelize / postgres并有一个表单(选择选项表单)。我试图让选项字段相互依赖,所以例如当选择一个特定的名字时,只有相应的姓氏在其他选择选项字段中显示。我真的不知道该怎么做,只发现了一些类似的例子,但仍然不明白如何直接从数据库中提取值,如何做到这一点,任何帮助将不胜感激。
Angular工厂:
app.factory('DoctorsFactory', function($http){
var DoctorsFactory={};
DoctorsFactory.fetchAll=function(){
return $http.get('/api/doctors')
.then(function(docs){
return docs.data;
})
}
return DoctorsFactory;
});
控制器:
app.controller('AppointmenController', function ($scope, DoctorsFactory) {
$scope.doctors={};
DoctorsFactory.fetchAll()
.then(function(all){
console.log('bla', all)
$scope.docs=all;
})
});
HTML:
<div class="container">
<h2>Form control: select</h2>
<form ng-submit="searchForDoc(doctors)">
<div class="form-group">
<label for="sel1">Firstname</label>
<select class="form-control" id="sel1" ng-model="doctors.firstname">
<option ng-repeat="doc in docs">{{doc.firstname}}</option>
</select>
<br>
<label for="sel1">Lastname</label>
<select class="form-control" id="sel1" ng-model="doctors.lastname">
<option ng-repeat="doc in docs">{{doc.lastname}}</option>
</select>
<br>
<label for="sel1">Speciality</label>
<select class="form-control" id="sel1" ng-model="doctors.speciality">
<option ng-repeat="doc in docs">{{doc.speciality}}</option>
</select>
<br>
<label for="sel1">Department</label>
<select class="form-control" id="sel1" ng-model="doctors.department">
<option ng-repeat="doc in docs">{{doc.department}}</option>
</select>
<br>
<label for="sel1">School</label>
<select class="form-control" id="sel1" ng-model="doctors.school">
<option ng-repeat="doc in docs">{{doc.school}}</option>
</select>
<br>
</div>
<input type="submit">
</form>
</div>
路线:
router.get('/', function(req,res,next){
Doctors.findAll({})
.then(function(alldoctors){
console.log(alldoctors)
res.send(alldoctors);
})
.catch(next);
})
数据库模型:
var Sequelize = require('sequelize');
var db = require('../_ db');
module.exports = db.define('doctors', {
firstname: {
type: Sequelize.STRING
},
lastname: {
type: Sequelize.STRING
},
speciality: {
type: Sequelize.STRING
},
department: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
school: {
type: Sequelize.STRING
},
languages:{
type: Sequelize.STRING
}
});