我正在尝试检索一个简单的JSON列表并将其注入下拉列表。
但是,我从以下JS收到错误。
var rac = angular.module('rac', ['angular.filter']);
rac.controller('prodCtrl', function($scope, $http, $sce, $filter){
var dataURL =
$sce.trustAsResourceUrl('http://www.mocky.io/v2/5b1ef454310000fa163ffa45');
$http({
method: 'JSONP',
url: dataURL
}).then(function ($scope, response) {
$scope.products.data = response;
console.log($scope.products);
}, function (response) {
console.log("Mr. Data. Report! " + response);
});
});
我的HTML:
<div class="row">
<div class="container">
<h1>Leonard Zakoor - Rolled Alloys </h1>
<div ng-controller="prodCtrl">
<h3>Please choose a product</h3>
<select>
<option ng-repeat="product in products">{{product.name}}</option>
</select>
</div>
</div>
我得到的错误是:TypeError:无法设置undefined的属性'data'
在第9行
$scope.products.data = response;
你需要定义$scope.products = [];
然后ng-repeat结束,
<option ng-repeat="product in products">{{product.name}}</option>
编辑:我对请求做了一些更改,如下所示,
rac.controller('prodCtrl', function($scope, $http, $sce, $filter) {
$http.jsonp($sce.trustAsResourceUrl("https://www.mocky.io/v2/5b1ef454310000fa163ffa45"), {
jsonpCallbackParam: 'callback'
})
.then(function(data) {
$scope.products = data.data.products;
},
function(error) {}
);
});