Angular $ resource get vs $ get

I use the Angular service $resourceand I don’t understand why there are two different methods for the main queries.

I can do it:

var House = $resource('/house/:uuid', {}); // create resource
var houseUuid = '123';
var house = new House.get({uuid: houseUuid}); // create resource instance

... and then in my controller:

$scope.house = house; // after get request to server data will be putted to model

BUT

There is a strange method in the resource instance $get

house.$get(...) // what is $get?

What is the difference? How can i use them? What is the main function of the method $get?

+4
source share
1 answer

In the class of $resourceavailable general methods 5 get, save, query, removeand delete, which may be caused directly over the class House.

save, remove delete $ House class/resource, CRUD .

Java-, 5 $ Java, 3 (save, remove delete) $ .

:

// Define a class/resource
var House = $resource('/house/:uuid', {});

// Get an instance
var houseInstance = Hourse.get({uuid: "xyz"});

// Delete the instance on any event like click using the `$` prefix (which works directly on the instance)
houseInstance.$delete()

// Or delete that instance using the class:
House.delete({uuid: houseInstance.uuid});

, save remove. , $get , . MVC, .

():

var House = $resource('/house/:uuid', {}, {
    foo: {
         method: "POST",
         url: "/house/show/:uuid"
    },
    update: {
        method: "PUT"
    }
});

:

House.foo({uuid: "xyz"}, {houseNumber: "1234"});

// Or you can write this:

var house = new House();
house.uuid = "xyz";
house.houseNumber = "1234";
house.$foo();
// Or any custom method
house.$update();

- , .. ( ), (.. $), House ( ), .

, House, House, . :

( ):

<div ng-repeat="house in houses">
    {{house.name}}
    <a href="house.$delete()">Delete this house</a>
</div>

( )

( ):

<div ng-repeat="house in houses">
    {{house.name}}
    <a href="deleteHouse(house.uuid)">Delete this house</a>
</div>

:

$scope.deleteHouse = function(uuid) {
    House.delete({uuid: uuid});
};

, , v.s. , .

+4

Source: https://habr.com/ru/post/1616200/


All Articles