查看localhost时的Angular Data绑定问题

时间:2017-07-18 18:11:56

标签: javascript angularjs mongodb data-binding database

所以,我现在有一个数据绑定角度问题,因为我正在使用MEAN Stack并且在我的数据库连接的情况下正常运行。我已经正确添加了JSON条目,只是从我的localhost查看时它们不会出现。



var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
    console.log("Hello World from controller");

var refresh = function(){
  $http.get('/eventlist/').then(function(response) {
    console.log("I got the data I requested");
    $scope.eventlist = response.data;
    $scope.event = null;
  });
};

refresh();

  $scope.addEvent = function () {
    console.log($scope.event);
    $http.post('/eventlist/', $scope.event).then(function(response){
      console.log(response);
      refresh();
    });
    $scope.update = function() {
      console.log($scope.event._id);
      $http.put('/eventlist/' + $scope.event._id, $scope.event).then(function(response) {
        refresh();
      })
    };
  };

  $scope.remove = function(id) {
    console.log(id);
    $http.delete('/eventlist/' + id).then(function(response) {
      refresh();
    });
  };

  $scope.edit = function(id) {
    console.log(id);
    $http.get('/eventlist/' + id).then(function(response) {
      $scope.event = response;
    });
  };


  $scope.deselect = function() {
    $scope.event = null;
    refresh();
  };

  }]);

body {
    position: relative;
    overflow-x: hidden;
    background-color: #00ffea;
}
body,
html { height: 100%;}
.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
  background-color: transparent;
}
paper-dialog.size-position {
    position: fixed;
    top: 16px;
    right: 16px;
    width: 300px;
    height: 300px;
    overflow: auto;
  }
.indent{
  margin-left: 25px;
}
/*-------------------------------*/
/*           Wrappers            */
/*-------------------------------*/

#wrapper {
    padding-left: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#wrapper.toggled {
    padding-left: 220px;
}

#sidebar-wrapper {
    z-index: 1000;
    left: 220px;
    width: 0;
    height: 100%;
    margin-left: -220px;
    overflow-y: auto;
    overflow-x: hidden;
    background: #1a1a1a;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#sidebar-wrapper::-webkit-scrollbar {
  display: none;
}

#wrapper.toggled #sidebar-wrapper {
    width: 220px;
}

#page-content-wrapper {
    width: 100%;
    padding-top: 70px;
}

#wrapper.toggled #page-content-wrapper {
    position: absolute;
    margin-right: -220px;
}

/*-------------------------------*/
/*     Sidebar nav styles        */
/*-------------------------------*/

.sidebar-nav {
    position: absolute;
    top: 0;
    width: 220px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.sidebar-nav li {
    position: relative;
    line-height: 20px;
    display: inline-block;
    width: 100%;
}

.sidebar-nav li:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    height: 100%;
    width: 3px;
    background-color: #1c1c1c;
    -webkit-transition: width .2s ease-in;
      -moz-transition:  width .2s ease-in;
       -ms-transition:  width .2s ease-in;
            transition: width .2s ease-in;

}
/*.sidebar-nav li:first-child a {
    color: #fff;
    background-color: #1a1a1a;
}
.sidebar-nav li:nth-child(2):before {
    background-color: #ec1b5a;
}
.sidebar-nav li:nth-child(3):before {
    background-color: #79aefe;
}
.sidebar-nav li:nth-child(4):before {
    background-color: #314190;
}
.sidebar-nav li:nth-child(5):before {
    background-color: #279636;
}
.sidebar-nav li:nth-child(6):before {
    background-color: #7d5d81;
}
.sidebar-nav li:nth-child(7):before {
    background-color: #ead24c;
}
.sidebar-nav li:nth-child(8):before {
    background-color: #2d2366;
}
.sidebar-nav li:nth-child(9):before {
    background-color: #35acdf;
}*/
.sidebar-nav li:hover:before,
.sidebar-nav li.open:hover:before {
    width: 100%;
    -webkit-transition: width .2s ease-in;
      -moz-transition:  width .2s ease-in;
       -ms-transition:  width .2s ease-in;
            transition: width .2s ease-in;

}

.sidebar-nav li a {
    display: block;
    color: #ddd;
    text-decoration: none;
    padding: 10px 15px 10px 30px;
}

.sidebar-nav li a:hover,
.sidebar-nav li a:active,
.sidebar-nav li a:focus,
.sidebar-nav li.open a:hover,
.sidebar-nav li.open a:active,
.sidebar-nav li.open a:focus{
    color: #fff;
    text-decoration: none;
    background-color: transparent;
}

.sidebar-nav > .sidebar-brand {
    height: 65px;
    font-size: 20px;
    line-height: 44px;
}
.sidebar-nav .dropdown-menu {
    position: relative;
    width: 100%;
    padding: 0;
    margin: 0;
    border-radius: 0;
    border: none;
    background-color: #222;
    box-shadow: none;
}

/*-------------------------------*/
/*       Hamburger-Cross         */
/*-------------------------------*/

.hamburger {
  position: fixed;
  top: 20px;
  z-index: 999;
  display: block;
  width: 32px;
  height: 32px;
  margin-left: 15px;
  background: transparent;
  border: none;
}
.hamburger:hover,
.hamburger:focus,
.hamburger:active {
  outline: none;
}
.hamburger.is-closed:before {
  content: '';
  display: block;
  width: 100px;
  font-size: 14px;
  color: #fff;
  line-height: 32px;
  text-align: center;
  opacity: 0;
  -webkit-transform: translate3d(0,0,0);
  -webkit-transition: all .35s ease-in-out;
}
.hamburger.is-closed:hover:before {
  opacity: 1;
  display: block;
  -webkit-transform: translate3d(-100px,0,0);
  -webkit-transition: all .35s ease-in-out;
}

.hamburger.is-closed .hamb-top,
.hamburger.is-closed .hamb-middle,
.hamburger.is-closed .hamb-bottom,
.hamburger.is-open .hamb-top,
.hamburger.is-open .hamb-middle,
.hamburger.is-open .hamb-bottom {
  position: absolute;
  left: 0;
  height: 4px;
  width: 100%;
}
.hamburger.is-closed .hamb-top,
.hamburger.is-closed .hamb-middle,
.hamburger.is-closed .hamb-bottom {
  background-color: #1a1a1a;
}
.hamburger.is-closed .hamb-top {
  top: 5px;
  -webkit-transition: all .35s ease-in-out;
}
.hamburger.is-closed .hamb-middle {
  top: 50%;
  margin-top: -2px;
}
.hamburger.is-closed .hamb-bottom {
  bottom: 5px;
  -webkit-transition: all .35s ease-in-out;
}

.hamburger.is-closed:hover .hamb-top {
  top: 0;
  -webkit-transition: all .35s ease-in-out;
}
.hamburger.is-closed:hover .hamb-bottom {
  bottom: 0;
  -webkit-transition: all .35s ease-in-out;
}
.hamburger.is-open .hamb-top,
.hamburger.is-open .hamb-middle,
.hamburger.is-open .hamb-bottom {
  background-color: #1a1a1a;
}
.hamburger.is-open .hamb-top,
.hamburger.is-open .hamb-bottom {
  top: 50%;
  margin-top: -2px;
}
.hamburger.is-open .hamb-top {
  -webkit-transform: rotate(45deg);
  -webkit-transition: -webkit-transform .2s cubic-bezier(.73,1,.28,.08);
}
.hamburger.is-open .hamb-middle { display: none; }
.hamburger.is-open .hamb-bottom {
  -webkit-transform: rotate(-45deg);
  -webkit-transition: -webkit-transform .2s cubic-bezier(.73,1,.28,.08);
}
.hamburger.is-open:before {
  content: '';
  display: block;
  width: 100px;
  font-size: 14px;
  color: #fff;
  line-height: 32px;
  text-align: center;
  opacity: 0;
  -webkit-transform: translate3d(0,0,0);
  -webkit-transition: all .35s ease-in-out;
}
.hamburger.is-open:hover:before {
  opacity: 1;
  display: block;
  -webkit-transform: translate3d(-100px,0,0);
  -webkit-transition: all .35s ease-in-out;
}

/*-------------------------------*/
/*            Overlay            */
/*-------------------------------*/

.overlay {
    position: fixed;
    display: none;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(250,250,250,.8);
    z-index: 1;
}

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <link rel="shortcut icon" href="images/favicon.ico"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="css/page.css">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

    <title>Summer Event Mapping</title>
    <meta name="description" content="Carnival and Event Mapping tool">

    <link rel="manifest" href="/manifest.json">

  </head>
  <body>
  <div id="wrapper">
      <div class="overlay"></div>

      <!-- Sidebar -->
      <nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
          <ul class="nav sidebar-nav">
              <li class="sidebar-brand">
                  <a href="#">
                     FILTERS
                  </a>
              </li>
              <li>
                  <a href="#">Enter Zip Code</a>
              </li>
              <li>
                  <input type="text" class="indent"/>
              </li>
              <li>
                  <a href="#">Festivals</a>
              </li>
              <li>
                  <a href="#">Carnivals</a>
              </li>
              <li>
                  <a href="#">Other Events</a>
              </li>
              <li>
                  <button class="indent">Apply Filters</button>
              </li>
              <li>
                  <a href="mailto:theapp@somemail.com">Contact</a>
              </li>
          </ul>
      </nav>
      <!-- /#sidebar-wrapper -->

      <!-- Page Content -->
      <div id="page-content-wrapper">
          <button type="button" class="hamburger is-closed" data-toggle="offcanvas">
              <span class="hamb-top"></span>
        <span class="hamb-middle"></span>
      <span class="hamb-bottom"></span>
          </button>



          <div class="container" ng-controller="AppCtrl">

        <h1 class="text-center">Summer Event List</h1>

        <table class="table">
          <thead>
            <tr>
              <th>Name</th>
              <th>Start Date</th>
              <th>End Date</th>
              <th>Price</th>
              <th>Location</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat="event in eventlist">
              <td>{{event.name}}</td>
              <td>{{event.startDate}}</td>
              <td>{{event.endDate}}</td>
              <td>{{event.price}}</td>
              <td>{{event.location}}</td>
            </tr>
          </tbody>
        </table>
      </div>

      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js" charset="utf-8"></script>
      <script src="controllers/controller.js" charset="utf-8"></script>


  </div>
   <!-- /#page-content-wrapper -->


   <script>
   $(document).ready(function () {
   var trigger = $('.hamburger'),
       overlay = $('.overlay'),
      isClosed = false;

     trigger.click(function () {
       hamburger_cross();
     });

     function hamburger_cross() {

       if (isClosed == true) {
         overlay.hide();
         trigger.removeClass('is-open');
         trigger.addClass('is-closed');
         isClosed = false;
       } else {
         overlay.show();
         trigger.removeClass('is-closed');
         trigger.addClass('is-open');
         isClosed = true;
       }
   }

   $('[data-toggle="offcanvas"]').click(function () {
         $('#wrapper').toggleClass('toggled');
   });
 });
   </script>

</body>

</html>
&#13;
&#13;
&#13;

&#13;
&#13;
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('eventlist', ['eventlist']);
var bodyParser = require('body-parser');


app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());

app.get('/eventlist', function(req, res) {
  console.log("i received a get request")

  db.eventlist.find(function(err, docs) {
    console.log(docs);
    res.json(docs);
  });
});


app.post('/eventlist', function(req, res) {
  console.log(req.body);
  db.eventlist.insert(req.body, function(err, doc) {
    res.json(doc);
  });
});

app.delete('/eventlist/:id', function(req, res) {
  var id = req.params.id;
  console.log(id);
  db.eventlist.remove({
    _id: mongojs.ObjectID(id)
  }, function(err, doc) {
    res.json(doc);
  });
});

app.get('/eventlist/:id', function(req, res) {
  var id = req.params.id;
  console.log(id);
  db.eventlist.findOne({
    _id: mongojs.ObjectId(id)
  }, function(err, doc) {
    res.json(doc);
  });
});

app.put('/eventlist/:id', function(req, res) {
  var id = req.params.id;
  console.log(req.body.name);
  db.eventlist.findAndModify({
    query: {
      _id: mongojs.ObjectId(id)
    },
    update: {
      $set: {
        name: req.body.name,
        startDate: req.body.startDate,
        price: req.body.price,
        location: req.body.location
      }
    },
    new: true
  }, function(err, doc) {
    res.json(doc);
  });
});


app.listen(4000);
console.log("Server running on port 4000");
&#13;
&#13;
&#13;

0 个答案:

没有答案