2016-10-17 74 views
1

我工作的一對應該當用戶點擊一個按鈕,從假的值更改爲true角度的功能。該應用跟蹤用戶喜歡的書籍;當用戶創建收藏夾時,「跟蹤」和「完成」的默認值設置爲false。當用戶去使用它們升級到真正的NG-點擊,新的「真」值未打補丁的數據庫,並在控制檯登錄爲靜止假。對我的功能缺少什麼想法?角功能不更新數據庫

$scope.trackFavorite = function(favorite) { 
     var favoriteParams = { 
     id: favorite.id, 
     tracking: favorite.tracking, 
     finished: favorite.finished 
     }; 
     favorite.tracking = !favorite.tracking; 
     $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams).success(function(response) { 
     console.log("READING NOW"); 
     console.log(response); 
     }); 
    }; 

    $scope.markFinished = function(favorite) { 
     var favoriteParams2 = { 
     id: favorite.id, 
     finished: favorite.finished, 
     }; 
     favorite.finished = !favorite.finished; 
     console.log(favorite); 
     $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams2).success(function(response){ 
     console.log("IS IT FINISHED"); 
     console.log(response); 
     }); 
    }; 

下面是從視圖中的NG-點擊片段,以防萬一:

<div> 
<button ng-class="{tracking: favorite.tracking}" ng-click="trackFavorite(favorite)">Reading Now</button> 
</div> 

<div> 
<button ng-class="{finished: favorite.finished}" ng-click="markFinished(favorite)">Finished</button> 
</div> 

非常感謝!

+1

你應該看看從你的承諾回來的錯誤,這將給你的背景,爲什麼它沒有正確保存。 – Makoto

回答

3

有可能是因爲你錯過了一些HTTP配置的機會。正如這裏已經注意到的那樣:patch request using angularjs

這也將是實現在控制器中的誤差函數的好主意,例如根據該響應更新的形式,即你回來。

$scope.trackFavorite = function(favorite) { 
     var favoriteParams = { 
     id: favorite.id, 
     tracking: favorite.tracking, 
     finished: favorite.finished 
     }; 

     $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams) 
      .then(
       function(response) { 
        console.log("READING NOW"); 
        console.log(response); 
        //update the UI according to the response 
        favorite.tracking = !favorite.tracking; 
       },function(error){ 
       //clean up when an error occurs 
       }); 

    };