html - 无法访问 html 模板中的 this.text
问题描述
我正在尝试添加两个按钮,它们应该只显示输入字段中是否有一些文本,否则它应该默认隐藏/禁用,但我收到了这个错误。
<div class="card card-body">
`<form>`
`<div class="form-group">`
`<input type="text" class="form-control" placeholder="Add a Log ...">`
`<input type="submit" value="Add Log" class="btn btn-light" [disabled]=!this.text>`
`<button class="btn btn-warning" [hidden]=!this.text>Clear </button>`
`</div>
`</form>
</div>
错误:清除~~~~~~~~~
src/app/components/log-form/log-form.component.ts:5:16
templateUrl: './log-form.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component LogFormComponent.
解决方案
您可以使用 ngModel 轻松实现这一点,如下所示:
mycomp.component.html
<form>
<div class="form-group">
<input type="text" class="form-control" name="text" [(ngModel)]="textvalue" placeholder="Add a Log ...">
<input type="submit" value="Add Log" class="btn btn-light" [disabled]="!textvalue">
<button class="btn btn-warning" [hidden]="!textvalue">Clear </button>
</div>
{{textvalue}}//contains value of your input element.
</form>
mycomp.component.ts
export class MyComponent {
textvalue:string;
}
确保在 app.module.ts 文件的 imports[] 数组中导入 FormsModule 以使 ngModel 工作。输入的名称属性也很重要,或者您可以使用 formControlName。
希望这可以帮助!!!
推荐阅读
- python - 如何在函数内使用机械化包登录网站?
- c - 创建整数列表并按升序打印它们的 C 程序
- rust - 是否可以通过命名参数来调用 Rust 中的函数?
- android - 活动到片段通信:当我尝试从活动更新片段中的文本视图时,出现空指针异常
- python-3.x - 如何更改当前运行的 chrome 驱动程序会话中的默认下载目录
- angularjs - 使用 AngularJS 绘制神经节图
- discord - Discordpy,Praw,获取链接的 img 预览,或者如果没有链接并且附加了一个 img,则嵌入该链接
- javascript - 如何在控制台中显示 ROCK、PAPER、SCISSOR GAME Javascript 的结果
- javascript - discord.js userinfo command show all roles
- visual-studio - C++ 多集错误 C2676:二进制“-”:“const _BidIt”未定义此运算符或转换为预定义运算符可接受的类型