如何将值传递给指令?(How to pass a value into the Directive?)
我有一个指令:
@Directive({ selector: 'random' }) export class RandomDirective { @Input text: string; }
我可以将值
text
传递给指令吗?<div random></div>
谢谢
I have a Directive:
@Directive({ selector: 'random' }) export class RandomDirective { @Input text: string; }
Can I pass a value
text
into the Directive?<div random></div>
Thanks
原文:https://stackoverflow.com/questions/37604052
更新时间:2021-07-12 17:07
最满意答案
试试这个:
<html> <head> <script type="text/javascript"> function CopyData(val){ var a = document.getElementById(val.id).value var inputs = document.querySelectorAll(".input"); for(var i=0;i < inputs.length;i++) { inputs[i].value = a; } } </script> </head> <body> Title:<input type="text" name ="Title" id="Text" onkeyup="CopyData(this)"/><br /> <br /> Input 1:<input type="text" class="input" name ="Input1" /><br /> Input 2:<input type="text" class="input" name ="Input2" /><br /> Input 3:<input type="text" class="input" name ="Input3" /><br /> </body> </html>
备注:
- 不要为多个元素使用相同的id。 尝试上课
- 使用未定义的“标题”,使用“a”,即存储输入值的位置
- 使用简单的js一次获得许多元素,好的方法是使用“querySelectorAll”和正确的选择器。
祝你好运。
try this out:
<html> <head> <script type="text/javascript"> function CopyData(val){ var a = document.getElementById(val.id).value var inputs = document.querySelectorAll(".input"); for(var i=0;i < inputs.length;i++) { inputs[i].value = a; } } </script> </head> <body> Title:<input type="text" name ="Title" id="Text" onkeyup="CopyData(this)"/><br /> <br /> Input 1:<input type="text" class="input" name ="Input1" /><br /> Input 2:<input type="text" class="input" name ="Input2" /><br /> Input 3:<input type="text" class="input" name ="Input3" /><br /> </body> </html>
remarks:
- do not use same id for multiple elements. try class instead
- you use 'Title' which is not defined, use "a", that was where you stored the input's value
- to get many elements at once with simple js, good method is to use "querySelectorAll" with the proper selector.
good luck.
相关问答
更多-
使用AJAX,PHP和Javascript自动填充输入文本框(auto fill input text boxes using AJAX and PHP and Javascript)[2021-12-14]
试试这个 echo json_encode(mysqli_fetch_assoc($result)); 在脚本循环中,控制台日志数据以查看结果以进行故障排除 将脚本更改为 $(document).on("change", "#locationSelectBox").function() { var id = $(this).val(); $.ajax({ url : "locationAjax.php", data : { "id" : id }, typ ... -
我可以停止100%宽度的文本框超出其容器?(Can I stop 100% Width Text Boxes from extending beyond their containers?)[2022-11-02]
您可以做的是删除input中的默认“extras”: input.wide {display:block; width:100%;padding:0;border-width:0} 这将保留input到其容器内。 现在,如果你想要边框,将input包装在div ,边框设置在div (这样你也可以从input删除display:block )。 就像是:试试这个: