angular - 如何在 html“值”属性中使用字符串插值
问题描述
我使用以下内容在 component.ts 文件中生成日期:
this.myDate=new Date();
并尝试在 html 表单值字段中使用。但没有加载任何内容。我尝试了以下语法但没有成功
<input type="datetime" class="form-control" id="date" value={{this.myDate}} name="created" ngModel>
<input type="datetime" class="form-control" id="date" [value]=[this.myDate] name="created" ngModel>
另一个问题是我的 json 文件在添加操作后只更新了空字符串
解决方案
您缺少输入类型 datetime 的正确日期格式,即 ="2017-06-01T08:30"
在 component.ts
formatted_date:string;
ngOnInit(){
//your code...
this.formatted_date = this.myDate.toISOString();
this.formatted_date.slice(0,16);
}
在 template.html 中(将类型更改为 datetime-local 因为 datetime 已过时MDN Doc)
<input type="datetime-local" class="form-control" id="date" value="{{formatted_date}}" name="created">
推荐阅读
- node.js - ReactJS:有没有办法在抽屉打开时避免子组件重新渲染?
- python - Flask 中有关 url_for 的查询
- php - 联系表格卡在验证中
- c# - XML解析C#中的子树
- javascript - 从 api 响应中获取 pdf 文件
- pandas - python pandas在方法链中划分数据框
- python - Python 2.x 是否总是为打印语句返回一个字符串?
- javascript - 如何回显人们在搜索框中输入的内容
- angular - 使用 CurrencyPipe 转换反应式表单的输入
- python - 检索在docker容器内运行python脚本的无效语法