[value] 和 value in Angular 有什么区别,检查 value 是否为 null 的方法是什么?

What is the difference between [value] and value in Angular and what is the method of checking value for null?

<mat-form-field>
    <input matInput placeholder="Organization name" [value]="organizationDetail.name"
        formControlName="organizationName" required>
</mat-form-field>

检查organizationDetail.name是否为空的方法是什么?我收到错误

错误类型错误:无法读取 null

的 属性 'name'

在我的 .ts 文件中,我将其初始化如下:

organizationDetail: any = {};

constructor() {
    this.organizationDetail = {
        address: '',
        attachmentId: null,
        createdBy: '',
        createdDateTime: '',
        industryFramework: [],
        invitation: [],
        location: '',
        name: '',
        orgAssessment: [],
        organizationId: 0,
        organizationPolicy: [],
        ownerId: 0,
        partnerType: null,
        partnerTypeId: 0,
        phone: '',
        updateBy: '',
        updateDateTime: '',
        user: [],
    };
}

提前感谢您的帮助。我找不到任何可以帮助我找出差异并解决错误的好文章。

Interpolation属性binding其实是一样的,bind-sr​​c只是另一种方式,比较啰嗦并且不经常使用。

差异:

interpolation "injects" 值到 html,所以当你说 value="{{ hello }}" Angular 是在你的变量之间插入括号。

属性 绑定 允许Angular 直接访问html 中的元素属性。这是更深层次的访问。当你说 [value]="hello" Angular 正在获取元素的值 属性,并将你的变量设置为 属性 的值。

事件绑定 允许您使用点击等事件来触发功能。这些绑定使用括号,例如 (click)="myFunction($event)"。这将调用 .ts 文件中定义的 myFunction() 方法。 '(click)' 周围的括号将函数绑定到 dom 事件。$event 是将事件对象传递给函数的关键字。您还可以传递带单引号的字符串,甚至是带插值的变量。

双向(数据)绑定允许您将事件与 属性 绑定相结合。例如

<input [(ngModel)]="username">
<p>Hello {{username}}!</p>    

将允许您同时输入和显示值。在这里了解更多

最后 何时使用插值以及何时使用数据绑定。这通常是一种形式,通常在使用智能组件和哑(演示)组件时,由于可读性,您将使用 属性 绑定绑定到 html,并且我应该说,"more secure" 在这种情况下绑定到 属性。如果您有简单的值,那么插值可能是您的朋友。这一切都归结为可读性、最佳实践和偏好。