我使用的是角$resource服务,我不清楚为什么主查询有两种不同的方法。
我可以这么做:
var House = $resource('/house/:uuid', {}); // create resource
var houseUuid = '123';
var house = new House.get({uuid: houseUuid}); // create resource instance然后,...and在我的控制器中:
$scope.house = house; // after get request to server data will be putted to model但
资源实例中存在奇怪的$get方法。
house.$get(...) // what is $get?有什么关系?我该怎么用呢?$get法的主要用途是什么?
发布于 2015-11-17 11:01:29
get、save、query、remove和delete 5种常用方法可以在$resource类中使用,这些方法可以通过House类直接调用。
而save、remove和delete方法可以在House类/资源实例上使用$前缀进行访问,这使得我们可以轻松地对任何实例执行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});
};这仅仅是一个简单的示例,它演示了何时使用实例操作vs.class操作的用例,并清楚地说明在上面的示例中使用实例操作将更加简洁。
https://stackoverflow.com/questions/33754896
复制相似问题