[英]Getting undefined when i try to access parameter
I am trying to access the parameter which is passed to the JavaScript function and alert the value. 我正在尝试访问传递给JavaScript函数的参数并提醒该值。 but when i alert the value i am getting
undefined
in alert. 但是当我警报值时,我在警报中变得
undefined
。 what is wrong i have done here. 我在这里做错了什么。
html html
<div ng-app="plusminusApp" ng-controller="categorylist">
<div class="whitescreen" id="buttons-overlay">
<div class="icons-container">
<div class="icons-inside-container">
<div ng-repeat="category in categories" value="{{category.picture}}" class="category-icon-thumbnail" onclick="setCategory(this.value);">
<img value="{{category.category}}" src="{{category.picture}}">
</div>
</div>
</div>
</div>
</div>
app.js app.js
function setCategory(category){
alert(category)
document.getElementById('buttons-overlay').style.display = 'none';
}
If you want to pass a angular's scope value to a function, it must be ng-
prefixed event. 如果要将角度的范围值传递给函数,则它必须是
ng-
前缀的事件。 You can't pass scope variable to a function outside of angular context. 您不能将范围变量传递给角度上下文之外的函数。
<div ng-repeat="category in categories" value="{{category.picture}}" class="category-icon-thumbnail" ng-click="setCategory(category.picture)">
and the function must be in the scope of the controller that means you need to define $scope.setCategory = function(val){}
并且该函数必须在控制器的范围内,这意味着您需要定义
$scope.setCategory = function(val){}
HTML: HTML:
<div ng-app="plusminusApp" ng-controller="categorylist">
<div class="whitescreen" id="buttons-overlay">
<div class="icons-container">
<div class="icons-inside-container">
<div ng-repeat="category in categories"class="category-icon-thumbnail" >
<img value="{{category.category}}" src="{{category.picture}}" ng-click="setCategory(category.picture);">
</div>
</div>
</div>
</div>
JS: JS:
$scope.setCategory = function(val){
console.log(val)
}
Assuming this to be your json 假设这是您的json
$scope.categories = [{
'category': 'ABC',
'picture': 'https://randomuser.me/api/portraits/men/22.jpg'
}, {
'category': 'ced',
'picture': 'https://randomuser.me/api/portraits/men/13.jpg'
}]
Use the below code 使用下面的代码
$scope.setCategory =function(category) {
$scope.something=category;
document.getElementById('buttons-overlay').style.display = 'none';
}
<div ng-app="plusminusApp" ng-controller="categorylist">
<p>Hello!</p>
<div class="whitescreen" id="buttons-overlay">
<div class="icons-container">
<div class="icons-inside-container">
<div ng-repeat="category in categories" value="{{category.picture}}" class="category-icon-thumbnail">
<button ng-click="setCategory(category.category)"> {{category.category}}
</button>
<img value="{{category.category}}" src="{{category.picture}}">
</div>
</div>
</div>
</div>
</div>
For simplicity and understanding purpose I have changed the click event from div to button. 为简单起见,我将click事件从div更改为button。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.