简体   繁体   中英

Pass a variable from view to controller in Express Angular

I have a view which displays List of my database and it works! All I want to do is to pass a variable named "search1" from view to server side Controller Thats ALL!

View

<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>

Client Controller

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();
}; }]);

Service

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

Server Controller

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);
        });
    } }

Server Route

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

app.route('/alls')
.get(alls.list);}

I should be able to see Jeans Model when it is chosen but it does not show me Jeans Model I think I have not passed "search1" to the server Controller properly and I need to change my client Controller or Server Route; But I do not know how!

Any idea or example would be helpful, Thanks!

I think you need to change

var search1 = req.search1;

to

var search1 = req.params.search1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM