如何监听输入的变化然后更改文本?

时间:2017-03-24 10:52:58

标签: javascript html angular

我在HTML中使用此代码:

<label for="date">START DATE:
    <input id="date" name="date" type="number"> <span>days</span>
<label>

现在我希望如果用户输入1作为input中的值span应该更改为day,并且如果它超过1 days天它应该保持*ngIf。如何使用eventEmitter.on('userSignedIn', function(socket, user) { socket.emit("userJoined", {currentUsers: user.first_name}); });

进行此操作

感谢。

2 个答案:

答案 0 :(得分:1)

<强>更新

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the current row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //get the values from the datarowview
        decimal val1 = Convert.ToDecimal(row["val1"]);
        decimal val2 = Convert.ToDecimal(row["val2"]);
        decimal val3 = Convert.ToDecimal(row["val3"]);

        decimal sum = val1 + val2 + val3;

        //find the label in the row with findcontrol
        Label lblTotal = e.Row.FindControl("Label1") as Label;

        //set the total value in the label with 2 decimal points
        lblTotal.Text = string.Format("{0:N2}", sum);
    }
}

<强>原始

<input id="date" name="date" type="number" #myInput>
<span>day{{myInput.value <= 1 ? '' : 's'}}</span>

使用此表单,您可以绑定到字段。如果该字段是getter / setter,那么您还可以在值更改时执行代码:

<input ngModel (ngModelChange)="doSomething($event)" id="date" name="date" type="number"> <span>days</span>

您也可以使用

<input [(ngModel)]="someprop"" id="date" name="date" type="number"> <span>days</span>

<input (change)="doSomething($event)" id="date" name="date" type="number"> <span>days</span>

答案 1 :(得分:1)

可以使用ngIf指令完成,但这似乎是错误的方法。 在我看来,你可以改变代码中的值,只显示在span标签中,但似乎你有使用ifs做某些任务

<input id="date" name="date" type="number" [(ngModel)]="selectedNumber">
<span *ngIf="selectedNumber==1">day</span>
<span *ngIf="selectedNumber!=1">day</span>