2014-04-05 3 views
2

내 나머지 통화에 내 변수 '되어 selectedDate'를 사용하기 위해 노력하고있어,하지만 항상 심지어 '() setSelectedDate'를 사용하여 새 값을 설정 한 후, 기존의 하나를 사용하는 기능ngResource의 기본 매개 변수

app.factory('MyService', function ($resource) { 

    var d = new Date(); 
    var curr_date = d.getDate(); 
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear(); 
    var selectedDate = curr_month + "/" + curr_date + "/" + curr_year; 

    return { 
     getSelectedDate : function(){ 
      return selectedDate; 
     }, 
     setSelectedDate : function(newDate){ 
      selectedDate = newDate; 
      console.log(selectedDate); // My setSelectedDate works well because I can see the new value in the console 
     }, 

     // The selectedDate always return the first value (the new Date()), and not the updated date 
     Meal: $resource('/rest/food/meal', {date : selectedDate, mealId: '@mealId'}, { 
      query: { 
       method: 'GET', 
       isArray: true, 
       params : {date : selectedDate} 

      }   
     }), 
    } 
}); 

내가 수동으로 내 컨트롤러에서 예를 들어, 다음과 같은 매개 변수를 설정하면 작동합니까 :

FoodService.setSelectedDate(newDate); 
$scope.meals = FoodService.Meal.query(FoodService.getSelectedDate()); 

하지만 자동으로 '되어 selectedDate'변수를 사용하여 내 서비스를하고 싶은, 그냥 전화를해야 :

FoodService.setSelectedDate(newDate); 
$scope.meals = FoodService.Meal.query(); 

하지만 항상 이전 날짜를 사용하고 업데이트 된 날짜는 사용하지 마십시오 ..

아이디어가 있으십니까?

편집 :

내가 PARAMS으로 시도하고, '/ 휴식/음식/식사'다음 후 $ 자원에 : - {날짜 : getSelectedDate} => ReferenceError가 : getSelectedDate이 을 정의되지 않은 - { 날짜 : getSelectedDate()} => 같은 - {날짜 : this.getSelectedDate()} => 형식 오류 : 개체 번호가있는 방법이 없습니다 'getSelectedDate'

마지막 .. 일을해야 하나 - {날짜 : selectedDate)} => 작동하지만 항상 처음에 selectedDate를 사용하지만 처음에 정의한 날짜는 사용하지 않습니다.

+0

공장에 추가 된 새로운 getSelectedDate() 함수를 보았습니까? API 이름과 혼동을 일으키는 경우 이름을 바꿀 수 있습니다. – dmahapatro

+0

도움을 주셔서 감사합니다. 기능 이름을 변경하려고했지만 여전히 동일한 문제가 있습니다. 내 매개 변수에서 정확히 무엇을 부를까요? getSelectedDate(), this.getSelectedDate(), selectedDate, others ..? – GSP59

답변

1

공장은 다음과 같이 수정해야합니다 :

app.factory('FoodService', function ($resource) { 
    var d = new Date(); 
    var curr_date = d.getDate(); 
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear(); 
    var selectedDate = curr_month + "/" + curr_date + "/" + curr_year; 

    function blahBlahFunction() { 
     return selectedDate; 
    } 

    return { 
     getSelectedDate : function(){ 
      return blahBlahFunction(); 
     }, 
     setSelectedDate : function(newDate){ 
      selectedDate = newDate; 
      console.log(selectedDate); 
     }, 

     Meal: $resource('/rest/food/meal', {date : blahBlahFunction, 
              mealId: '@mealId'}, { 
      query: { 
       method: 'GET', 
       isArray: true, 
       params : { date : blahBlahFunction } 
      }   
     }), 
    } 
}); 

참고 :
PARAMS 기본 사전 구속 매개 변수입니다. 매개 변수를 동적으로 제공하기 위해 요소는 함수에 래핑되어야하고 함수는 매개 변수로 사용되어야합니다.

FoodService.Meal.query({date: "12/11/2014"}); 

이 내용은이 PLUNKER을 참조하십시오이 거의 기본 PARAM의 개념을 무시

, 나는 위의 호출로했을 변경 없이 생각합니다. 콘솔의 dev 도구에서 URL 오류를 찾으십시오. 말해야한다 :

GET http://run.plnkr.co/rest/food/meal?date=12%2F11%2F2014 404 (Not Found) 
+0

당신은 바로 다음과 같습니다 : FoodService.Meal.query ({date : "12/11/2014"}); 내 코드가 인데 내 창을 기본 매개 변수로 'selectedDate'를 사용할 수 없습니다. '/ rest/food/meal'뒤에 $ resources에서 params로 시도했습니다. - ReferenceError : getSelectedDate가 정의되지 않았습니다. - {date : getSelectedDate()} => same - {date : 이.Object # 에는 'getSelectedDate'메서드가 없습니다. 그리고 작동 할 것입니다 - {date : selectedDate}} => 항상 처음에 selectedDate를 사용하지만 처음에는 정의하지 않습니다. updated date – GSP59

+0

[plunker] (http://plnkr.co/edit/Plx7sqRA3Jq9KItA1yYX?p=preview)를 참조하고 콘솔의 dev 도구에서 url 오류를 찾으십시오. – dmahapatro

+0

다시 한번 공장에서 추가 한 새 기능을 찾습니다. – dmahapatro