简体   繁体   English

Angular:如何在HTML getElementById中使用变量

[英]Angular: How to use variable in HTML getElementById

I am using ng-repeat to create multiple divs with modals, which I open by getElementById. 我正在使用ng-repeat创建带有模式的多个div,这些模式由getElementById打开。 When I hardcode the id (eg 'modalId'), it works but not if I try to use a variable. 当我对ID进行硬编码(例如'modalId')时,它可以工作,但是如果我尝试使用变量则不能。 How do I make it work? 我该如何运作?

<div class="col-sm-4 col-md-4" ng-repeat="p in proj">
        <img class="myImg" ng-src="{{p.image}}" onclick="document.getElementById('{{p.id}}').style.display='block';">
        <div id="{{p.id}}" class="w3-modal" onclick="this.style.display='none';">
          <div class="w3-modal-content w3-animate-zoom">
            <img ng-src="{{p.image}}">
          </div>
        </div>
</div>

The p.id I want to use (line 2) is an int. 我要使用的p.id(第2行)是一个整数。

From the docs: Interpolations for HTML DOM event attributes are disallowed. 从文档中:不允许对HTML DOM事件属性进行插值。 Please use the ng- versions (such as ng-click instead of onclick) instead. 请改用ng-版本(例如ng-click而不是onclick)。

See here: https://docs.angularjs.org/error/$compile/nodomevents . 看到这里: https : //docs.angularjs.org/error/$compile/nodomevents

Example: 例:

$scope.imgClicked = function(id){
    document.getElementById(id).style.display='block';
}

HTML: HTML:

<img class="myImg" ng-src="{{p.image}}" ng-click="imgClicked(p.id)">

check this 检查这个

https://docs.angularjs.org/error/ $compile/nodomevents https://docs.angularjs.org/error/ $ compile / nodomevents

you must be getting the below warning in your console. 您必须在控制台中收到以下警告。

Interpolations for HTML DOM event attributes are disallowed. 不允许对HTML DOM事件属性进行插值。

This error occurs when one tries to create a binding for event handler attributes like onclick, onload, onsubmit, etc. 当尝试为事件处理程序属性(如onclick,onload,onsubmit等)创建绑定时,会发生此错误。

There is no practical value in binding to these attributes and doing so only exposes your application to security vulnerabilities like XSS. 绑定这些属性没有任何实际价值,这样做只会使您的应用程序面临XSS之类的安全漏洞。 For these reasons binding to event handler attributes (all attributes that start with on and formaction attribute) is not supported. 由于这些原因,不支持绑定到事件处理程序属性(以on和formaction属性开头的所有属性)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM