문제

lubanlockServices.factory('UserConfig', ['$resource',
    function($resource){
        return $resource('user/config/:item', {item: '@item'});
    }
]);

lubanlockControllers.controller('NavCtrl', ['$scope', 'UserConfig',
    function($scope, UserConfig){
        $scope.minimized = UserConfig.get({item: 'nav_minimized'});

        $scope.$watch('minimized', function(value){
            console.log(value);
        });

        $scope.toggleMinimize = function(){
            $scope.minimized = !$scope.minimized;
            UserConfig.save({item: 'nav_minimized'}, $scope.minimized);
        }
    }
]);

The problem is, when $scope.minimized is false, the request will not contain any post data, rather than sending a JSONified false. That causes problem saving value in server side.

Any solution? Or is there something wrong with my usage?

도움이 되었습니까?

해결책

JSON is Javascript Object Notation, you really should be POSTing (and retrieving) an object. Ideally, it would look like:

$scope.config = UserConfig.get({item: 'nav_minimized'}); 
//returns a $resource with data {minimized: false}

$scope.$watch('config.minimized', function(value){
    console.log(value);
});
$scope.toggleMinimize = function(){
    $scope.config.minimized = !$scope.config.minimized;
    $scope.config.$save(); //and then you can do this to save
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top