[英]AngularJS error : TypeError: Cannot read property 'images' of undefined
AngularJS: v1.5.11 AngularJS:v1.5.11
Im trying to display images from a JSON array. 我试图显示来自JSON数组的图像。
Is this the proper way : $scope.mainImage = $scope.template.images[0].name;
这是正确的方法$scope.mainImage = $scope.template.images[0].name;
: $scope.mainImage = $scope.template.images[0].name;
. 。 This is the line in the error that it says cant read property of images
. 这是错误中的行,它表示无法读取images
属性。
My Controller in templates.js file: 我的控制器在templates.js文件中:
.controller("TemplatesDetailsCtrl", ["$scope", "$routeParams", "$http", "$filter", function($scope, $routeParams, $http, $filter ){
var templateId = $routeParams.templateID;
$http.get("json/templates.json").success(function(data){
$scope.template = $filter("filter")(data, function(d){
return d.id == templateId;
})[0];
$scope.mainImage = $scope.template.images[0].name; //THIS IS LINE 30
});
}]);
I'm trying to display the very first image from the array. 我正在尝试显示数组中的第一张图像。 So my HTML is: 所以我的HTML是:
<img class="img-full" src="img/{{mainImage}}">
Ben trying a lot and not able to display the image. 本尝试了很多但无法显示图像。 Im getting this error: 我收到此错误:
Please help. 请帮忙。
Seems like 'template' object is undefined. 似乎未定义“模板”对象。 You can test for undefined
before trying to use its properties : 您可以在尝试使用undefined
的属性之前对其进行测试:
if ($scope.template)
$scope.mainImage = $scope.template.images[0].name; //THIS IS LINE 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.