简体   繁体   English

AngularJS错误:TypeError:无法读取未定义的属性“图像”

[英]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.

相关问题 AngularJS + Fullcalendar发送错误TypeError:无法读取未定义的属性&#39;__id&#39; - AngularJS + Fullcalendar send error TypeError: Cannot read property '__id' of undefined 错误TypeError:无法读取未定义的属性&#39;名称&#39;-使用angularjs - ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs 使用 AngularJS 和 Ionic 出现错误“TypeError:无法读取未定义的属性‘email’” - Error "TypeError: Cannot read property 'email' of undefined" using AngularJS and Ionic AngularJS 1.4错误-未捕获的TypeError:无法读取未定义的属性“ apply” - AngularJS 1.4 error - Uncaught TypeError: Cannot read property 'apply' of undefined 类型错误:无法使用 AngularJS 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with AngularJS AngularJS:TypeError:无法读取未定义的属性“childNodes” - AngularJS : TypeError: Cannot read property 'childNodes' of undefined angularJS)TypeError:无法读取未定义的属性“ push” - angularJS) TypeError: Cannot read property 'push' of undefined TypeError:无法读取angularjs中未定义的属性“ reduce” - TypeError: Cannot read property 'reduce' of undefined in angularjs AngularJS - TypeError:无法读取未定义的属性'go' - AngularJS - TypeError: Cannot read property 'go' of undefined Angularjs / Ionic TypeError:无法读取未定义的属性&#39;then&#39; - Angularjs/Ionic TypeError: Cannot read property 'then' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM