로 리디렉션되지는 app.js 파일을 여기에
var CarApp = angular.module('CarApp',['ngResource'])
CarApp.config(function($routeProvider){
$routeProvider
.when('/',{controller:ListCtrl,templateUrl:'partials/list.html'})
.when('/edit/:id',{controller:EditCtrl,templateUrl:'partials/details.html'})
.otherwise({redirectTo:'/'})
});
// to map update method
CarApp.factory('CarsService',function($resource){
return $resource('/api/cars/:id',{id:'@_id'} , {update:{method:'PUT'}})
});
function EditCtrl($scope,$location,$routeParams,CarsService){
var id = $routeParams.id;
//console.log(id);
CarsService.get({id: id},function(resp){
$scope.car = resp;
});
// Update Page title
$scope.action = "Update";
$scope.save = function() {
CarsService.update({id:id},$scope.car,function(){
$location.path('/')
});
}
}
내 AngularJS와 코드입니다 .html 중에서 코드
다음<h2>{{action}} Ferrari</h2>
<form name="carform" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="year">Year</label>
<div class="controls">
<input type="text" ng-model="car.year" id="year" name="year" placeholder="">
</div>
</div>
<div class="form-actions">
<button ng-click="save()" class="btn btn-primary">
Update
</button>
<a href="/" class="btn">Cancel</a>
</div>
</form>
내 MongoDB의 백엔드 서비스
입니다function update(req,res){
var newCarData = req.body;
var id = req.params.id;
newCarData._id = ObjectId(id);
updateCar(newCarData,function(err){
if(err) throw new Error("unable to update");
else{
res.json();
}
});
}
function updateCar(car,callback){
db.collection(collectionName,function(error,collection){
if(error) callback(true);
else {
collection.save(car,function(){
//console.log("updated data") ;
});
}
});
}
내가 직면 한 문제는 details.html의 업데이트 버튼을 누르면 mongodb 백엔드 서비스의 세부 정보를 업데이트 할 수 있다는 것입니다.
은 콘솔에서 put 메소드가 호출됩니다하지만 난 AngularJS와 app.js 파일에 $의 location.path ('/')를 사용하여 '/'경로에를 리디렉션 할 수 있습니까? 어떤 도움을 주시면 감사하겠습니다.
Hva 당신은 다음과 같이 put 메소드의 콜백에 매개 변수를 넣으려고했습니다. $ scope.save = function() { CarsService.update ({id : id}, $ scope.car, function (resp) { $ location.path ('/') }); } –
예 매개 변수 resp, 행운이 아직 작동하지 않음 – shaunak1111