[英]How to pass variable from html view to angular controller to access by scope
[英]Pass a variable from view to controller in Express Angular
我有一個顯示我的數據庫列表的視圖,它可以工作! 我要做的就是將一個名為“ search1”的變量從視圖傳遞到服務器端Controller Thats ALL!
視圖
<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);}
選擇它后,我應該能夠看到Jeans Model,但是它沒有顯示給我Jeans Model我認為我沒有正確地將“ search1”傳遞給服務器Controller,因此我需要更改客戶端Controller或Server Route。 但是我不知道怎么做!
任何想法或示例都會有所幫助,謝謝!
我認為你需要改變
var search1 = req.search1;
至
var search1 = req.params.search1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.