2017-08-31 50 views
1

我已经在控制器中创建阵列和用于angularjs视图在下拉该值如何以保存选择的下拉值MongoDB中

控制器:

$scope.menus = ['Home','WorkFlow','Statistics','Users','Projects','Setting']; 

视图:

<select name="selectedRole" ng-model="selectedRole" ng-options ="x.role for x in roles"> 
    <option value="">Role</option> 
</select> 

在服务器.js我写了保存功能

app.post('/addUser', function(req, res) { 

userCollection.save(req.body, (err, result) => { 
    if (err) return console.log(err) 

    console.log('Roles Added'); 
    res.redirect('/users') 
    console.log('result') 

    }) 

}) 

但我不知道如何将选定的Dropdown值保存在我的mongodb中。我正在使用mongoclient

请问任何人都可以帮我解决这个问题。

回答

1

我固定我的问题。

<select name="role" ng-model="role" > 
        <option ng-repeat="role_type in roles" value="{{role_type._id}}">{{role_type.role}}</option> 
        </select> 

我用NG-重复,而不是NG选项

+1

然后,当您提出问题时,您应该稍微清楚一点。 – t1gor

+0

当然,谢谢..其实我是编码新手。我会提高自己提出问题。 – Vishnu

0

如果你的下拉是这样的:

<select id="thedropdown"> 
    <option value="1">one</option> 
    <option value="2">two</option> 
</select> 

,那么你会使用类似:

var a = document.getElementById("thedropdown"); 
alert(a.options[a.selectedIndex].value); 

但像jQuery库简化了的东西:

alert($('#thedropdown').val()); 
+0

这不是server.js代码 – t1gor

+0

这就是你如何能做到这一点的JS – WebGuy

+0

从问题:'但是我不知道如何保存在我的mongodb中选定的Dropdown值' – t1gor

2

我会想想下面这样的事情:

app.post('/addUser', function(req, res) { 

userCollection.save(req.body, (err, result) => { 
    if (err) return console.log(err) 

    const MongoClient = require('mongodb').MongoClient; 

    MongoClient.connect(db.url, (err, database) => { 
     if (err) return console.log(err) 

     database.collection('users').insert({ 
     user: <blah-blah>, 
     role: req.body.role 
     }, (err, result) => { 
      if (err) { 
      res.send({ 'error': 'An error has occurred' }); 
      } else { 
      console.log('Roles Added'); 
      res.redirect('/users') 
      } 
     }); 
    }) 
    }) 

}) 

请注意这段代码远不是生产状态,只是一个例子。它还假定您正在使用mondodb npm软件包进行连接。要安装,运行:npm install mongodb --save

希望有所帮助。

Documentation