【问题标题】:Search in Database and Display result Express Node Angular MEAN.js $resource在数据库中搜索并显示结果 Express Node Angular MEAN.js $resource
【发布时间】:2015-08-05 19:58:40
【问题描述】:

我有两个模特,名为“牛仔裤”和“衬衫” 其中有两个变量“名称”和“颜色”

我想要一个搜索页面,用户可以在其中搜索数据库以查找特定颜色或名称的衬衫和牛仔裤。

这个链接可能是一个提示,但我就是想不通 Similar Problem

我这里有东西但是它不起作用。 如果您告诉我如何解决它,我将不胜感激。谢谢!

查看

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

<div class="Search">

    <h2>Search Section</h2>

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

    <select data-ng-model="search2" id="search">
      <option value="Model1">Jeans</option>
      <option value="Model2">Shirts</option>
    </select>

    <select data-ng-model="variable1" id="variable1">
      <option selected="selected">AnyThing</option>
      <option value="color1">Blue</option>
      <option value="color2">Red</option>
    </select>

    <select data-ng-model="variable2" id="variable2">
      <option selected="selected">AnyThing</option>
      <option value="name1">John</option>
      <option value="name2">Bob</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>Name</th>
                <th>Color</th>
            </tr>
        </thead>
        <tbody>
                <tr data-ng-repeat="all in alls">   
                <td data-ng-bind="all.name"></td>
                <td data-ng-bind="all.color"></td>
                </tr>
        </tbody>
    </table>

</div>

在控制器中;首先,我查看用户选择了哪些属性并将其分配给 FindArray 然后我看到用户想要搜索哪个模型。(AnyThing 表示用户没有选择任何内容)

服务器端控制器

exports.list = function(req, res) {
if (variable1 === "AnyThing")
{
    if (variable2 === "AnyThing")
    {
        FindArray = {};
    }else
    {
        FindArray = { name = variable2 };
    }
}else
{
    FindArray = { color = variable1 , name = variable2 };
}

if (req.body.search1 === 'Jeans')
{

    if (req.body.search2 === 'Shirts')
    {
        Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
            Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
                var all = shirts.concat(jeans);
                res.jsonp(all);
            });
        });
    }else{
        Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
                res.jsonp(jeans);
        });
    }
}else{
    Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
            res.jsonp(shirts);
    });
}
};

$资源服务:

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

客户端控制器

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

        $scope.search1 = search1;
        $scope.search2 = search2;
        $scope.variable1 = variable1;
        $scope.variable2 = variable2;

        $scope.alls = Alls.query();

    };
}]);

服务器端路由

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

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

【问题讨论】:

  • 它怎么不完全工作?你期望发生什么没有发生?您是否从客户端获取服务器上的表单数据?是数据库查找不起作用还是什么?
  • 这是关于控制器我猜我的控制器无法识别 search1 和 search2 以及 variable1 和 variable2 当我添加这些行时,我的应用程序没有显示任何内容!我需要将数据从我的视图发送到控制器吗?因为我在路由中只有 GET,我认为我需要将这些变量发送到我的控制器。当我注释掉这些变量时,我的视图显示整个数据库意味着我可以从数据库读取到我的视图所以我认为我需要一些东西来将数据从我的视图发送到控制器
  • 如果您有任何以这种方式工作的简单搜索页面,那将是一个很大的帮助
  • 没有 plunkr 可能很难调试,但 AllsController 中的这段代码是错误的:$scope.search1 = search1; $scope.search2 = search2; $scope.variable1 = variable1; $scope.variable2 = variable2; 右侧的那些变量不存在。 HTML 视图假定引用的变量已经在 $scope 中,因此这是隐含的。

标签: javascript angularjs node.js express meanjs


【解决方案1】:

在我看来,对此的初步审查就像您的路线期待获得,但您的 $resource 正在运行看跌期权。我还认为您的所有范围分配都不起作用,因为您分配了不存在的变量。

【讨论】:

    猜你喜欢
    • 2013-02-23
    • 2015-08-12
    • 2010-11-24
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多