嗨,我一直收到这个错误。我试图使破碎的图像图标不出现在Angular Ionic应用程序中。我有tis功能,但它一直说anonImg没有定义。我不知道如何定义这个?
function anonImg(image){
image.src = 'https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg';
}

我的html是
<ion-content class="climb-pics">
<img ng-src="{{urlForImage(selected_.images)}}"/>
<img onerror="anonImg(this);" ng-src="{{urlForImage(selected_.imageTwo)}}"/>
<button type="button" class="button button-assertive" ng-click="closeModal(1)">Close</button>
<p> </p>
</ion-content>
&#13;
我应该将函数包装在函数中吗?当我收到错误时,我现在不知道该做什么&#34; ReferenceError:anonImg未定义&#34;
这是我的控制器代码:
.controller('ClimbController', ['$scope', '$stateParams', 'Products', 'Climbs', '$ionicModal', '$localStorage', '$cordovaCamera', '$cordovaFile',
function($scope, $stateParams, Products, Climbs, $ionicModal, $localStorage, $cordovaCamera, $cordovaFile) {
$scope.product_id = $stateParams.productId;
console.log('ClimbController: ' + $scope.product_id);
// ClimbsController: 2
$scope.urlForImage = function(imagePath) {
console.log('imagePath: ' + imagePath);
return Products.urlForImage($scope.product_id, imagePath);
}
var filename = Products.getFilenameFromLocalStorage($scope.product_id);
console.log(filename);
var climbId = $stateParams.climbId;
var areaId = $stateParams.areaId;
if (!isNaN(climbId)) {
climbId = parseInt(climbId);
}
if (!isNaN(areaId)) {
areaId = parseInt(areaId);
}
$scope.selected_ = {};
$scope.items = [];
$scope.details = true;
$scope.data = '';
$scope.noteParams = {
text: null
};
// looping though all data and get particular product
$scope.selectClimb = function(areas){
areas.forEach(function(data) {
if(data._id == climbId){
$scope.selected_ = data;
}
});
}
// get all posts // try some function to get a single produt from server
$scope.getPosts = function(){
Products.loadGuidebook(filename).then(function(data) {
// NOTE: data = the whole feed.json file
console.log(data);
// data = feed.json file
var climbs = [];
data.areas.map(function(area) {
if (area._id === areaId) {
climbs = area.climbs;
}
});
$scope.selectClimb(climbs);
},
function (error) {
console.log(error);
});
}
// picture slider logic
$scope.allImages = $scope.images;
//hide div with broken images
anonImg = function(image){
image.src = 'https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg';
}
$scope.load();
$scope.getPosts();
}
])
答案 0 :(得分:0)
你还没有发布控制器代码,你的控制器应该有$ scope,函数应该被定义为,
$scope.anonImg = function(image){
image.src = 'https://instagramimages-a.akamaihd.net/profiles/anonymousUser.jpg';
}