2017-08-31 30 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