【发布时间】:2016-09-10 17:29:21
【问题描述】:
我正在阅读角度教程STEP 13 REST and Custom Services,但不确定是否理解正确。
这里创建一个工厂来封装$http。聪明的部分是有一个文件phones/phones.json 包含电话列表,phones/<phoneId>.json 文件包含每个电话的详细信息。
angular.
module('core.phone').
factory('Phone', ['$resource',
function($resource) {
return $resource('phones/:phoneId.json', {}, {
query: {
method: 'GET',
params: {phoneId: 'phones'},
isArray: true
}
});
}
]);
因此,当他想要获取电话的完整列表时,使用Phone.query() 是有意义的,因为工厂调用Phone 并且里面有一个名为query: 的东西(该标签是如何调用的?方法?功能?)。由于没有任何参数作为参数,我猜phoneId 从GET 声明中获取phones 的默认值。
angular.
module('phoneList').
component('phoneList', {
templateUrl: 'phone-list/phone-list.template.html',
controller: ['Phone',
function PhoneListController(Phone) {
this.phones = Phone.query();
this.orderProp = 'age';
}
]
});
但现在最后一部分不知道如何工作。我知道从$routeParams 获得phone_id。但是Phones 工厂现在如何拥有get() 函数,为什么query() 没有时需要回调函数;
angular.
module('phoneDetail').
component('phoneDetail', {
templateUrl: 'phone-detail/phone-detail.template.html',
controller: ['$routeParams', 'Phone',
function PhoneDetailController($routeParams, Phone) {
var self = this;
self.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) {
self.setImage(phone.images[0]);
});
}
]
});
所以我的三个问题是:
query的默认参数如何工作电话如何拥有
get()以及它如何接收参数为什么
get()需要回调函数而query()不需要
【问题讨论】:
标签: javascript angularjs json rest