I have a Jersey Rest Application which I d'like to access with a AngularJS application.
我有一个泽西休息应用程序,我喜欢使用AngularJS应用程序访问。
I have defined the resource as follows:
我已经将资源定义如下:
Service:
服务:
angular.service('Wine', function ($resource) {
return $resource('/myserver/rest/wines/:wineId', {}, {
update: {method:'PUT'},
'query': {method: 'GET', isArray: true}
});
});
Controller:
控制器:
function WineListCtrl(Wine) {
this.wines = Wine.query();
}
In Firebug I can see the result of this GET-Request:
在Firebug中,我可以看到此GET-Request的结果:
{"wine":[{"country":"USA","description":"With hints of ginger and spice, this wine makes an excellent complement to light appetizer and dessert fare for a holiday gathering.","grapes":"Pinot Noir","id":"9","name":"BLOCK NINE","picture":"block_nine.jpg","region":"California","year":"2009"},{"country":"Argentina","description":"Solid notes of black currant blended with a light citrus make this wine an easy pour for varied palates.","grapes":"Pinot Gris","id":"11","name":"BODEGA LURTON","picture":"bodega_lurton.jpg","region":"Mendoza","year":"2011"},{"country":"France","description":"The aromas of fruit and spice give one a hint of the light drinkability of this lovely wine, which makes an excellent complement to fish dishes.","grapes":"Grenache / Syrah","id":"1","name":"CHATEAU DE SAINT COSME","picture":"saint_cosme.jpg","region":"Southern Rhone / Gigondas","year":"2009"},{"country":"France","description":"Though dense and chewy, this wine does not overpower with its finely balanced depth and structure. It is a truly luxurious experience for the\nsenses.","grapes":"Merlot","id":"7","name":"CHATEAU LE DOYENNE","picture":"le_doyenne.jpg","region":"Bordeaux","year":"2005"},{"country":"France","description":"The light golden color of this wine belies the bright flavor it holds. A true summer wine, it begs for a picnic lunch in a sun-soaked vineyard.","grapes":"Merlot","id":"8","name":"DOMAINE DU BOUSCAT","picture":"bouscat.jpg","region":"Bordeaux","year":"2009"},{"country":"USA","description":"Though subtle in its complexities, this wine is sure to please a wide range of enthusiasts. Notes of pomegranate will delight as the nutty finish completes the picture of a fine sipping experience.","grapes":"Pinot Noir","id":"10","name":"DOMAINE SERENE","picture":"domaine_serene.jpg","region":"Oregon","year":"2007"},{"country":"Spain","description":"A resurgence of interest in boutique vineyards has opened the door for this excellent foray into the dessert wine market. Light and bouncy, with a hint of black truffle, this wine will not fail to tickle the taste buds.","grapes":"Tempranillo","id":"2","name":"LAN RIOJA CRIANZA","picture":"lan_rioja.jpg","region":"Rioja","year":"2006"},{"country":"France","description":"Breaking the mold of the classics, this offering will surprise and undoubtedly get tongues wagging with the hints of coffee and tobacco in\nperfect alignment with more traditional notes. Breaking the mold of the classics, this offering will surprise and\nundoubtedly get tongues wagging with the hints of coffee and tobacco in\nperfect alignment with more traditional notes. Sure to please the late-night crowd with the slight jolt of adrenaline it brings.","grapes":"Chardonnay","id":"12","name":"LES MORIZOTTES","picture":"morizottes.jpg","region":"Burgundy","year":"2009"},{"country":"USA","description":"The cache of a fine Cabernet in ones wine cellar can now be replaced with a childishly playful wine bubbling over with tempting tastes of\nblack cherry and licorice. This is a taste sure to transport you back in time.","grapes":"Sauvignon Blanc","id":"3","name":"MARGERUM SYBARITE","picture":"margerum.jpg","region":"California Central Cosat","year":"2010"},{"country":"USA","description":"A one-two punch of black pepper and jalapeno will send your senses reeling, as the orange essence snaps you back to reality. Don't miss\nthis award-winning taste sensation.","grapes":"Syrah","id":"4","name":"OWEN ROE \"EX UMBRIS\"","picture":"ex_umbris.jpg","region":"Washington","year":"2009"},{"country":"USA","description":"One cannot doubt that this will be the wine served at the Hollywood award shows, because it has undeniable star power. Be the first to catch\nthe debut that everyone will be talking about tomorrow.","grapes":"Pinot Noir","id":"5","name":"REX HILL","picture":"rex_hill.jpg","region":"Oregon","year":"2009"},{"country":"Italy","description":"Though soft and rounded in texture, the body of this wine is full and rich and oh-so-appealing. This delivery is even more impressive when one takes note of the tender tannins that leave the taste buds wholly satisfied.","grapes":"Sangiovese Merlot","id":"6","name":"VITICCIO CLASSICO RISERVA","picture":"viticcio.jpg","region":"Tuscany","year":"2007"}]}
In my html page I d'like to access the content as following:
在我的html页面中,我喜欢访问以下内容:
<ul ng:controller="WineListCtrl">
<li ng:repeat="wine in wines">
<a href='#/wines/{{ wine.id }}'>{{ wine.name }}</a>
</li>
</ul>
Sadly there are no results showing up in the UI (although the JSON Result seems to be okay and there is no error on the Browser Console).
遗憾的是,UI中没有显示任何结果(尽管JSON结果似乎没问题,并且浏览器控制台上没有错误)。
If I make an alert like
如果我发出警报就好
alert(this.wines.length)
I get 0 as result.
结果得到0。
Would be great to get some help of you guys...
很高兴得到你们的帮助......
Greets Marc
迎接马克
1 个解决方案
#1
7
Your first problem is that you're assigning to this
and not the scope. If you don't assign to the scope, your data won't be available for binding in the view. So inject the $scope
into your controller and assign it the wines
attribute:
你的第一个问题是你要分配给这个而不是范围。如果未分配范围,则视图中的数据将无法用于绑定。因此,将$ scope注入您的控制器并为其分配葡萄酒属性:
function WineListCtrl($scope, Wine) {
$scope.wines = Wine.query();
}
You have another problem though. The ngResource
module expects the result of call to query
to be an array, but you're actually returning an object:
你有另一个问题。 ngResource模块期望调用查询的结果是一个数组,但实际上你正在返回一个对象:
{wine: [an array of wines]}
So unless you remove the key from the server-side response, your ng-repeat
might have to look something like:
因此,除非您从服务器端响应中删除密钥,否则您的ng-repeat可能必须类似于:
<li ng-repeat="wine in wines.wine">...</li>
#1
7
Your first problem is that you're assigning to this
and not the scope. If you don't assign to the scope, your data won't be available for binding in the view. So inject the $scope
into your controller and assign it the wines
attribute:
你的第一个问题是你要分配给这个而不是范围。如果未分配范围,则视图中的数据将无法用于绑定。因此,将$ scope注入您的控制器并为其分配葡萄酒属性:
function WineListCtrl($scope, Wine) {
$scope.wines = Wine.query();
}
You have another problem though. The ngResource
module expects the result of call to query
to be an array, but you're actually returning an object:
你有另一个问题。 ngResource模块期望调用查询的结果是一个数组,但实际上你正在返回一个对象:
{wine: [an array of wines]}
So unless you remove the key from the server-side response, your ng-repeat
might have to look something like:
因此,除非您从服务器端响应中删除密钥,否则您的ng-repeat可能必须类似于:
<li ng-repeat="wine in wines.wine">...</li>