2013-09-21 2 views
0

로 리디렉션되지는 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 ('/')를 사용하여 '/'경로에를 리디렉션 할 수 있습니까? 어떤 도움을 주시면 감사하겠습니다.

+0

Hva 당신은 다음과 같이 put 메소드의 콜백에 매개 변수를 넣으려고했습니다. $ scope.save = function() { CarsService.update ({id : id}, $ scope.car, function (resp) { $ location.path ('/') }); } –

+0

예 매개 변수 resp, 행운이 아직 작동하지 않음 – shaunak1111

답변

2

docs에 따르면 $location은 브라우저 URL이 변경 될 때 전체 페이지를 다시로드하지 않습니다. URL을 변경 한 후 페이지를 다시로드하려면 하위 수준의 API 인 $window.location.href을 사용하십시오.

+0

$ window.location.href ('/') 아직 리디렉션되지 않음 – shaunak1111

+0

일반 Javascript'location.href'을 사용해 보셨나요? 그게 효과가 없다면, 당신은 다른 곳에 문제가 있다는 것입니다. – jtblin

+0

예 location.href를 시도했는데 문제가 무엇인지 알 수 없습니다. – shaunak1111

0

제어기 ListCtrl을 만들 필요가 없습니다. 웹 속성에서 ListCtrl을 찾을 수 없다는 오류가 발생했을 수 있습니다.

+0

ListCtrl을 가지고 ListCtrl 코드를 붙여 넣지 않았습니다. – shaunak1111

0

, 당신은 성공 시도에서 콜백 호출하지 않는 당신의 updateCar 함수처럼 보이는 :

function updateCar(car,callback){ 
    db.collection(collectionName,function(error,collection){ 
    if(error) callback(true); 

    else { 
     collection.save(car,function(){ 
      //console.log("updated data") ; 
      callback(null, { ok: true }); //something like this. 
     }); 

    } 
    }); 
} 

가 그래서 location.path를 호출하는 편집 Ctrl 키에 콜백,

라고되고 있지 않았다입니다. : D