2015-05-25 35 views
0

我有一个显示我的数据库列表的视图,它的工作原理! 我想要做的就是将一个名为“search1”的变量从视图传递到服务器端控制器这就是所有!在Express Angular中将一个变量从视图传递给控制器​​

查看

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

我应该能看到牛仔裤型号在选择时,但它并没有告诉我牛仔裤型号 我想我还没有通过“搜索1”到服务器控制器正常我需要更改我的客户端控制器或服务器路由; 但我不知道如何!

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

回答

0

我认为你需要改变

var search1 = req.search1; 

var search1 = req.params.search1; 
+0

它不工作! 你不觉得我需要通过$ post方法在客户端控制器中传递数据吗? 如果您有任何可行的示例,这将是非常棒的! –

+0

嗯,我认为这是一个给定的。你应该像这样使用Angular的$ http服务:$ http.post('/ someurl',{search1:'Jeans'}) –

+0

谢谢!你能告诉我应该在哪里写吗? 我不希望搜索是恒定的,我希望它可以根据用户的选择进行更改 –

相关问题