首页 \ 问答 \ 如何将值传递给指令?(How to pass a value into the Directive?)

如何将值传递给指令?(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>

备注:

  1. 不要为多个元素使用相同的id。 尝试上课
  2. 使用未定义的“标题”,使用“a”,即存储输入值的位置
  3. 使用简单的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:

  1. do not use same id for multiple elements. try class instead
  2. you use 'Title' which is not defined, use "a", that was where you stored the input's value
  3. to get many elements at once with simple js, good method is to use "querySelectorAll" with the proper selector.

good luck.

相关问答

更多
  • 试试这个 echo json_encode(mysqli_fetch_assoc($result)); 在脚本循环中,控制台日志数据以查看结果以进行故障排除 将脚本更改为 $(document).on("change", "#locationSelectBox").function() { var id = $(this).val(); $.ajax({ url : "locationAjax.php", data : { "id" : id }, typ ...
  • 您可以做的是删除input中的默认“extras”: input.wide {display:block; width:100%;padding:0;border-width:0} 这将保留input到其容器内。 现在,如果你想要边框,将input包装在div ,边框设置在div (这样你也可以从input删除display:block )。 就像是:
  • 试试这个: