如何使用angularjs的$ http服务使用节点js代码从mongodb获取数据

问题描述 投票:-1回答:1

//这是我的角度应用程序现在我想在json中使用节点如何使用$ http服务获取mongodb数据

            <tr ng-repeat="x in ItemsByPage[currentPage] | orderBy:columnToOrder:reverse">
               <td  >{{x.id}}</td>     // how to get data here from a json data of mongodb
               <td  >
                  <!-- editable username (text with validation) -->
                  <span editable-text="x.name" e-name="name" e-form="rowform" onbeforesave="checkName($data, x.id)" e-required>
                  {{ x.name || 'empty' }}
                  </span>
               </td>
               <td>
                  <!-- editable status (select-local) -->
                  <span editable-text="x.lName" e-name="lName" e-form="rowform">
                  {{ x.lName || 'empty' }}
                  </span>
               </td>
               <td >
                  <!-- editable group (select-remote) -->
                  <span editable-text="x.pages" e-name="pages" e-form="rowform" >
                  {{ x.pages || 'empty' }}
                  </span>
               </td>
               <td>
                  <span editable-text="x.passw1" e-name="passw1" e-form="rowform" >
                  {{x.passw1 || 'empty'}}
                  </span>
               </td>
               <td style="white-space: nowrap">
                  <!-- form -->
                  <form editable-form name="rowform"  ng-show="rowform.$visible" class="form-buttons form-inline">
                     <button type="submit" ng-disabled="rowform.$waiting" class="btn btn-primary">
                     save
                     </button>
                     <button type="button" ng-disabled="rowform.$waiting" ng-click="rowform.$cancel()" class="btn btn-default">
                     cancel
                     </button>
                  </form>
                  <div class="buttons" ng-show="!rowform.$visible">
                     <button class="btn btn-primary" ng-click="rowform.$show()">edit</button>
                     <button class="btn btn-danger" ng-click="removeUser($index)">del</button>
                  </div>
               </td>
            </tr>
         </table>
         <button class="btn btn-default" ng-click="addUser()">Add row</button>
         <br>
         <ul class="pagination pagination-sm">
            <li ng-class="{active:0}"><a href="#" ng-click="firstPage()">First</a>
            </li>
            <li ng-repeat="n in range(ItemsByPage.length)"> <a href="#" ng-click="setPage()" ng-bind="n+1">1</a>
            </li>
            <li><a href="#" ng-click="lastPage()">Last</a>
            </li>
         </ul>
      </div>
        <button type="button"  onclick="loadDoc(this.value)">Change Content</button>
    <!--    <div id="txtHint">Customer info will be listed here...</div> -->  
        <a href="/view-feedbacks">View Records</a>
      <script>
         var app = angular.module("app", ["xeditable", "ngMockE2E"]);


          app.service('filteredListService', function () {
              this.searched = function (valLists,toSearch) {
        return _.filter(valLists, 
        function (i) {
            /* Search Text in all 3 fields */
            return searchUtil(i, toSearch);
        });        
    };

             this.paged = function (valLists,pageSize)
            {
                retVal = [];
                for (var i = 0; i < valLists.length; i++) {
                    if (i % pageSize === 0) {
                        retVal[Math.floor(i / pageSize)] = [valLists[i]];
                    } else {
                        retVal[Math.floor(i / pageSize)].push(valLists[i]);
                    }
                }
                return retVal;
            };

         });

         app.run(function(editableOptions) {
          editableOptions.theme = 'bs3';
         });

         app.controller('Ctrl', function($scope, $filter, filteredListService) {
         $scope.users = [
         {id:1, name:'harry potter', lName:"Pege" ,passw1:"12/12/2012",pages:"556"},
         {id:2, name:'narnia',  lName:"Pim" ,passw1:"12/12/2012",pages:"557"},
         {id:3, name:'panchtantra',  lName:"Smith" ,passw1:"1/03/2009",pages:"556"},
         {id:4, name:'atlas', lName:"Jones" ,passw1:"2/04/1995",pages:"888"},
         {id:5, name:'science', lName:"Doe" ,passw1:"2/04/1995",pages:"888"},
         {id:6, name:'guiness book',lName:"Pan" ,passw1:"2/04/1995",pages:"888"},
              {id:7, name:'panchtantra1',  lName:"Smith" ,passw1:"1/03/2009",pages:"556"},
         {id:8, name:'atlas1', lName:"Jones" ,passw1:"2/04/1995",pages:"888"},
         {id:9, name:'science1', lName:"Doe" ,passw1:"2/04/1995",pages:"888"},
         {id:10, name:'guiness book1',lName:"Pan" ,passw1:"2/04/1995",pages:"888"},
            ];
         $scope.checkName = function(data, id) {
            if (id === 2 && data !== 'narnia') {
              return "Username 2 should be `narnia(case sensitive)`";
            }
          };

          $scope.saveUser = function(data, id) {
            //$scope.user not updated yet
            angular.extend(data, {id: id});
            return $http.post('/saveUser', data);
          };

          // remove user
          $scope.removeUser = function(index) {
            var index1 = index + $scope.currentPage * 4;
              $scope.users.splice(index1,1);
                $scope.pagination();   
               };

          // add user
          $scope.addUser = function($event) {
              $scope.currentPage = 2;
              $scope.id= $scope.users.length + 1
         $scope.users.push({

            id:this.id,
            name:'Enter Book Name',
            lName:'Author Name',
             passw1:'Date of Publish',
              pages:'Pages'

        });
                $scope.pagination(); 
              alert(users.id);
        $scope.resetAll();
    }


          //search
          $scope.pageSize = 4;

            $scope.allItems = $scope.users; 
            $scope.reverse = false;

            $scope.resetAll = function () {
                $scope.filteredList = $scope.allItems;
                $scope.newEmpId = '';
                $scope.newName = '';
                $scope.newEmail = '';
                $scope.searchText = '';
                $scope.currentPage = 0;
                $scope.Header = ['','',''];
            }
         //pagination
        $scope.pagination = function () {
        $scope.ItemsByPage = filteredListService.paged( $scope.filteredList, $scope.pageSize );         
    };

    $scope.setPage = function () {
        $scope.currentPage = this.n;
    };

    $scope.firstPage = function () {
        $scope.currentPage = 0;
    };

    $scope.lastPage = function () {
        $scope.currentPage = $scope.ItemsByPage.length - 1;
    };

    $scope.range = function (input, total) {
        var ret = [];
        if (!total) {
            total = input;
            input = 0;
        }
        for (var i = input; i < total; i++) {
            if (i != 0 && i != total - 1) {
                ret.push(i);
            }
        }
        return ret;
    };
   $scope.sort = function(sortBy){
        $scope.resetAll();  
       $scope.pagination();    
    };
    $scope.sort ('name');  


            $scope.search = function () {
        $scope.filteredList = 
       filteredListService.searched($scope.allItems, $scope.searchText);

        if ($scope.searchText == '') {
            $scope.filteredList = $scope.allItems;
        }
        $scope.pagination(); 
    }


            $scope.resetAll();       
         });

         function searchUtil(x,toSearch)
         {
            /* Search Text in all 3 fields */
            return ( x.name.toLowerCase().indexOf(toSearch.toLowerCase()) > -1 || x.lName.toLowerCase().indexOf(toSearch.toLowerCase()) > -1 || x.id == toSearch
                                    )                              
                             ? true : false ;
         }

      </script>
   </body>
</html>

//这里是我的nodejs file.how从我的angularjs中获取/ view-feedbacks中的数据。我是一个tyro to this.please只帮助我使用$ http服务。之后如何使用ng-repeat来获取该数据在表中。

app.get('/view-feedbacks',  function(req, res) {
    dbConn.then(function(db) {
        var dd=db.db("book");
        dd.collection('users').find({}).toArray().then(function(feedbacks) {
            res.status(200).json(feedbacks);
        });
    });
});



app.listen(process.env.PORT || 3100, process.env.IP || '0.0.0.0' );
angularjs node.js
1个回答
0
投票

你的nodejs代码是否正确。

app.get('/view-feedbacks',  function(req, res) {
    dbConn.then(function(db) {
        var dd=db.db("book"); // not sure of this line, it depends on how you started.
        dd.collection('users').find().toArray().then(function(err, feedbacks) { // the first parameter will always be err 
            console.log("feedbacks",feedbacks);//check whether you are getting feedbacks from your mongodb
            res.send(feedbacks); //no need for status here
        });
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.