简体   繁体   English

当我尝试访问参数时变得不确定

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

LIVE DEMO 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在下面的代码中访问db.collection时,为什么会得到未定义的信息? - Why am I getting an undefined when I try to access db.collection in the code below? 为什么当我尝试访问我在 node.js 中的财产时,我变得不确定 - why I am getting undefined when I try to access on my property in node js 尝试从标签获取数据时获取未定义的值 - Getting an undefined value when I try to get data from a label 为什么我尝试访问异步函数时会返回一个未定义的? - Why my async function returns an undefined when i try to access it? 当我尝试访问javascript数组时,正在填充未定义的值 - Undefined value is being populated when i try to access javascript array 当我尝试通过索引访问数组元素时,它给了我未定义的 - when i try to access array element by index it gives me undefined 当我尝试通过JS访问ViewData时遇到编译错误 - Getting a compilation error when I try and access ViewData through JS 每当我尝试访问 record.username 时,我都会收到“TypeError: Cannot read property 'username' of undefined” - I am getting "TypeError: Cannot read property 'username' of undefined" whenever I try to access record.username 尝试循环条件为什么我变得不确定 - Try to loop condition why i getting undefined 我尝试访问数组元素但未定义 - I try to access the array elements but get undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM