将下拉值而不是实际字符串按角度传递到节点



我有一个下拉列表,它是根据我创建的jSON对象填充的。我正试图传递文本值"0";葡萄酒供应商";作为下拉列表中的文本选择,但它将POST中的数组值传递给Node。

因此,如果我的下拉列表有以下选项:

  • B

  • C

  • D

    并且我选择";C";如果2的值正在被传递;C";

wines.ejs:中的代码段

            <form action="/createWine" method="POST">
                <p>Select the Wine Supplier:</p>
                <select name="wineSupplier" ng-model="supplierSelection" ng-options="supplier as supplier.supName for supplier in suppliers">
                </select>
                <label>Wine Name:</label>
                <input type="text" name="wineName" placeholder="Wine Name"/>
                <label>Wine Producer:</label>
                <input type="text" name="wineProducer" placeholder="Wine Producer"/>
                <label>Wine Colour:</label>
                <input type="text" name="wineColour" placeholder="Wine Colour"/>
                <label>Wine Type:</label>
                <input type="text" name="wineType" placeholder="Wine Type"/>
                <label>Wine Country:</label>
                <input type="text" name="wineCountry" placeholder="Wine Country"/>
                <p>
                    <button type="submit" class="btn">Submit</button>
                </p>
            </form>

来自app.js 的代码段

//Create a new wine objhect
app.post('/createWine', function(request, response) {
    //create and save a wine model
    var wine = new myWine({
        wineSupplier: request.body.wineSupplier,
        wineName: request.body.wineName,
        wineProducer: request.body.wineProducer,
        wineColour: request.body.wineColour,
        wineType: request.body.wineType,
        wineCountry: request.body.wineCountry
    });
    //save to model
    wine.save(function(err, model) {
        if (err) {
            response.send(504, 'There was an error');
        }
        else {
            response.redirect('/');
        }
    });
});

如果希望<option>的实际值和<select>本身是模型中的值,则应使用track by

如果没有track by,AngularJS将给出<options>数值,用于跟踪哪些选择属于哪些数组项。

angular.module('app', [])
.controller('C', ['$scope', function ($scope) {
  $scope.suppliers = [{supName:'A'},{supName:'B'}, {supName:'C'}, {supName:'D'}];
}]);
                
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-app="app" ng-controller="C">
  <select name="wineSupplier" ng-model="supplierSelection" 
          ng-options="supplier.supName for supplier in suppliers track by supplier.supName">
  </select>
</div>

另一方面,使用Angular只提供表单的一部分,而使用普通提交来提交值,这似乎有点滥用Angular。理想情况下,所有控件都应该绑定到模型中的字段,并且应该使用AngularJS来执行向服务器的提交。如果这样做,就不需要使用track by

最新更新