我正在尝试检索一个简单的JSON列表并将其注入下拉列表。
但是,我从以下JS收到错误。
console.log('Event created: %s', event.data.htmlLink)
我的HTML:
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);
});
});
我得到的错误是: TypeError:无法设置未定义的属性“data”
第9行
<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>
答案 0 :(得分:1)
您需要定义 $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) {}
);
});
<强> WORKING DEMO 强>