简体   繁体   English

如何将变量值从HTML传递到Angular

[英]How to pass variable value from HTML to Angular

I am trying to pass variable value from HTML to Angular. 我试图将变量值从HTML传递给Angular。 Something like as shown below: 如下所示:

<span (click)= "toggle()" toggled = "true" class="glyphicon glyphicon-unchecked"></span>

And in controller: 并在控制器中:

@Input() toogled: any;

 toggle () {
   console.log(this.toogled);
   -----
   -----
}

When I am doing console.log I am getting result as undefined. 当我做console.log时,我得到的结果是未定义的。

NOTE: I know that it is possible to create own directive and attach a new property to directive and use something like: 注意:我知道可以创建自己的指令并将新属性附加到指令并使用类似以下内容:

<my-directive [toggled] = "true"></my-directive>

But I want to know if its some work around to bind the properties that element doesn't have. 但是我想知道是否可以解决绑定该元素没有的属性的问题。

There might some other way but you can try two way data binding like this 可能还有其他方法,但是您可以尝试两种方式的数据绑定,如下所示

<span (click)="toggle()" [(ngModel)]="toggled" class="glyphicon glyphicon-unchecked"></span>

in ts file you have to define: 在ts文件中,您必须定义:

public toggled: boolean

use method toggle() to toggle the value 使用方法toggle()来切换值

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

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