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