S
S
sparcmen2016-03-17 00:56:14
Angular
sparcmen, 2016-03-17 00:56:14

How to save json CRUD with $resource (angularjs)?

The file reads and does not want to save changes, writes - "404 Not Found"! Why is that?! Who in the subject tell me :)
Service:

function CRUDservice($log, $resource, $http) {

        this.getData = function(callback) {
            var User = $resource('data/main/users.json');
            var dataUsers = User.get(callback);
        }

        this.saveData = function(newUser) {
            var User = $resource('data/main/users.json');
            var dataUsers = User.get(function() {
                dataUsers.users[0].name = newUser.name;                
                dataUsers.$save();
            });
        }
    }

Controller:
function MainCtrl($scope, $rootScope, $log, CRUDservice ) {

            var vm = this;
            $rootScope.curPath = 'main';

            CRUDservice.getData(function(_data) {
                vm.users = _data.users;
            });

            var newUser = {
                name: "Valera",
                age: 54
            }

            CRUDservice.saveData(newUser );
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GTRxShock, 2016-03-17
@GTRxShock

It was a long time ago, but I bookmarked a good guide on $resource
www.sitepoint.com/creating-crud-app-minutes-angula...
And how to deal with resources, I advise you to look at https://github.com/mgonto/ restangular

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question