2015-11-30 118 views
0

我一直在關注Thinkter Mean Stack教程,它的工作原理非常奇妙。所以我創建了自己的項目,到目前爲止一切正常。 但是,他們沒有介紹如何刪除帖子。 我可以爲我的生活不知道如何從數據中刪除元素。Thinkster MEAN堆棧教程,刪除元素

AngularApp.js

var app = angular.module('KOL', ['ui.router']) 
    .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) { 
     $stateProvider 
      .state('home', { 
       url: '/home', 
       templateUrl: "/views/home.ejs", 
       controller: 'kolCtrl', 
       resolve: { 
         patientPromise: ['patients', function(patients) { 
          return patients.getAll(); 
         }] 
        } 
      }) 
      .state('details', { 
       url: '/details/{id}', 
       templateUrl: './views/details.html', 
       controller: 'detailsCtrl' 
      }); 

      $urlRouterProvider.otherwise('home'); 
    }]) 
    .factory('patients', ['$http', function($http){ 
     var object = { 
     patients: []    

     }; 

     object.getAll = function() { 
      return $http.get('/patients').success(function(data) { 
       angular.copy(data, object.patients); 
      }); 
     } 

     object.create = function(patient) { 
     return $http.post('/patients', patient).success(function(data){ 
      object.patients.push(data); 
     }); 
    } 


    }; 
     return object; 
    }]) 
    .controller('kolCtrl', ['$scope', 'patients', 
     function($scope, patients){ 

       $scope.patients = patients.patients; 
       $scope.selectedItem = $scope.patients[0]; 


       $scope.addPost = function() { 
       if(!$scope.title || $scope.title === '') { return; } 
       if(!$scope.age || $scope.age === '') { return; } 
       patients.create({ 
        name: $scope.title, 
        age: $scope.age, 
       }) 

       $scope.title = ''; 
       $scope.age = ''; 

       }; 



       object.delete = function(patient)    

    }]) 
    .controller('detailsCtrl', [ 
     '$scope', 
     '$stateParams', 
     'patients', 
     function($scope, $stateParams, patients){ 


     $scope.patient = patients.patients[$stateParams.id]; 
    }]) 


; 

Home.ejs

<div class="container"> 
     <div clas="row"> 

      <div style="width: 200px; margin-top: 100px"> 
      <select ng-model="selectedItem" ng-options="patients.name for patients in patients" class="pull-left form-control" name="Vælg"></select> 
      </div> 
      <div class="viewbox pull-right"> 
       <h3>Patient: {{selectedItem.name}}</h3> 
       <p>Age: {{selectedItem.age}} </p> 
       <p>index: {{patients.indexOf(selectedItem)}}</p> 
       <button><a href="#/details/{{patients.indexOf(selectedItem)}}">Rediger</a></button> 
       <button ng-click="deleteItem(patients.indexOf(selectedItem))">Delete</button> 
      </div> 





     </div> 
     <div class="row" class="pull-left"> 
      <div style="width: 200px; margin-top: 100px"> 
      <form role="form" class="form-group" ng-submit="addPost()"> 
       <input class="form-control" type="text" ng-model="title" /> 
       <input class="form-control" type="text" ng-model="age" /> 
       <button type="submit">Add</button> 
      </form> 
      </form> 
     </div> 
    </div> 
    </div> 

index.js(路線get和post)

var express = require('express'); 
var router = express.Router(); 



/* GET home page. */ 
router.get('/', function(req, res, next) { 
    res.render('index', { title: 'Express' }); 
}); 

var mongoose = require('mongoose'); 
var Patient = mongoose.model('Patient'); 

router.get('/patients', function(req, res, next) { 
    Patient.find(function(err, patients){ 
    if(err){ return next(err); } 

    res.json(patients); 
    }); 
}); 

router.post('/patients', function(req, res, next) { 
    var patient = new Patient(req.body); 

    patient.save(function(err, post){ 
    if(err){ return next(err); } 

    res.json(patient); 
    }); 
}); 

router.delete('/patients/:patient', function(req, res, next) { 
    req.patient.remove(function(err, patient){ 
     if (err) { return next(err); } 
     res.json(patient); 
    }); 
}); 

router.param('patient', function(req, res, next, id) { 
    var query = Patient.findById(id); 

    query.exec(function (err, patient){ 
    if (err) { return next(err); } 
    if (!post) { return next(new Error('can\'t find patient')); } 

    req.patient = patient; 
    return next(); 
    }); 
}); 

router.get('/details/:patient', function(req, res) { 
    res.json(req.patient); 
}); 

module.exports = router; 

我懷疑答案很簡單,因爲在其他代碼,但可能不是? 謝謝。

+0

你能告訴我們正在處理您的請求JS文件?你已經發布了angularjs文件,但是向我們展示了應該有你的get和post函數的node.js/express.js文件。 – inspired

+0

感謝您的回答。我編輯過,所以它現在包含index.js,其中包含路線。這有幫助嗎? – Jacob

回答

0
object.delete = function(patient) { 
     return $http.delete('/patients', patient).success(function(data){ 
      for(var i = 0; i < object.patients.length; i++) { 
       if(object.patients[i].id == patient.id) { 
        object.patients.splice(i, 1); 
       } 
     }); 

這樣的事情也許很難說,不知道答案。如果響應(數據)是被刪除的患者,那麼你可以使用「如果(object.patients [I] .ID == 數據 .ID)」,而不是

+0

我上傳了響應數據。這是否更有意義? :) 謝謝回答。還試圖使用你發佈的內容。我會告訴你! – Jacob

1

根據你的函數要傳遞到router.deleteindex.js文件:

router.delete('/patients/:patient', function(req, res, next) { 
    req.patient.remove(function(err, patient){ 
     if (err) { return next(err); } 
     res.json(patient); 
    }); 
}); 

你必須使用與$http服務delete動詞時至patients.id追加到URL。所以,你可以在你的病人工廠增加一個delete方法對象:

.factory('patients', ['$http', function($http){ 
    var object = { 
    patients: []    

    }; 

    object.getAll = function() { 
     return $http.get('/patients').success(function(data) { 
      angular.copy(data, object.patients); 
     }); 
    } 

    object.create = function(patient) { 
    return $http.post('/patients', patient).success(function(data){ 
     object.patients.push(data); 
    }); 
    } 

    //add patient id to the url 
    object.delete = function(patient) { 
    return $http.delete('/patients/',patient).success(function(data){ 
     console.log(data); 
    }); 
    } 
} 


}; 
    return object; 
}]) 
+0

它說我不能做一個函數(patient.id)(意外的令牌) – Jacob

+0

現在它說:object.delete = function(patient.id),但它會在我刷新console.log時拋出一個錯誤該頁面(意外的令牌在第40行)第40行是object.delete = function(patient.id)。這是對的嗎? – Jacob

+0

我認爲部分問題是,元素(mongodb _id)的索引不是從ng-click傳遞到控制器 – Jacob