在Express Angular中,把一个变量从视图传递给控制器

Pass a variable from view to controller in Express Angular

本文关键字:变量 一个 视图 控制器 Angular Express      更新时间:2023-09-26

我有一个视图,显示我的数据库列表,它的工作原理!我所要做的就是传递一个名为"search1"的变量从视图到服务器端控制器这就是全部!

视图

<section data-ng-controller="AllsController" data-ng-init="find()">
<div class="page-header">
    <h1>Alls</h1>
</div>

<div class="Search">
    <select data-ng-model="search1" id="search">
      <option value="Model1">Jeans</option>
      <option value="Model2">Shirts</option>
    </select>
</div>

<br></br><br></br>
<h2>Result Section</h2>
<div class="list-group">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Created</th>
                <th>User</th>
                <th>Brand</th>
                <th>Color</th>
            </tr>
        </thead>
        <tbody>
                <tr data-ng-repeat="all in alls">
                <td data-ng-bind="all.created | date:'medium'"></td>
                <td data-ng-bind="all.user.displayName"></td>
                <td data-ng-bind="all.name"></td>
                <td data-ng-bind="all.color"></td>
                </tr>
        </tbody>
    </table>

</div>

客户机控制器

angular.module('alls').controller('AllsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Alls', 'Jeans', function($scope, $stateParams, $location, Authentication, Alls, Jeans) {
    $scope.find = function() {
          $scope.alls = Alls.query();
}; }]);

服务
angular.module('alls').factory('Alls', ['$resource',
function($resource) {
    return $resource('alls/:allId', { allId: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);
控制器服务器

exports.list = function(req, res) {
var search1 = req.search1 ;
    if (search1 === 'Jeans' )
    {
        Jeans.find().sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
            res.jsonp(jeans);
        });
    } }

服务器路由

module.exports = function(app) {
var alls = require('../../app/controllers/alls.server.controller');
app.route('/alls')
.get(alls.list);}

我应该能够看到牛仔裤模型,当它被选中,但它没有显示我牛仔裤模型我认为我没有传递"search1"到服务器控制器正确,我需要改变我的客户端控制器或服务器路由;但我不知道怎么做!

任何想法或例子都会有所帮助,谢谢!

我认为你需要改变

var search1 = req.search1;

var search1 = req.params.search1;