无法在'Element'上执行'setAttribute':']'不是有效的属性名称.角4

Dre*_*208 23 angular

我收到以下错误:

Failed to execute 'setAttribute' on 'Element': ']' is not a valid attribute name.
Run Code Online (Sandbox Code Playgroud)

我简单地做了模特:

export interface ModalComponentModel {
    username: string;
    password: string;
    password2: string;
    description: string;
}
Run Code Online (Sandbox Code Playgroud)

我在我的组件中使用它:

零件:

model: ModalComponentModel;
Run Code Online (Sandbox Code Playgroud)

然后我尝试在我的html文件中使用它:

    <div class="modal-header">
    <h4 class="modal-title text-center">Edit Profile</h4>
    <button type="button" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <div class="row">
        <div class="col-md-2 col-md-offset-2">
            <form class="form" role="form" (ngSubmit)="whatDoesThisFormDo()">
                <div class="form-group">
                    <label for="username">Username</label>
                    <input type="text" class="form-control" name="username" id="username" placeholder="Your username"
                           [(ngModel)]="model.username">
                </div>
                <div class="form-group">
                    <label for="password1">Password</label>
                    <input type="password" class="form-control" name="password1" id="password1" placeholder="Your password"
                           [(ngModel)]="model.password">
                </div>
                <div class="form-group">
                    <label for="password2">Confirm Password</label>
                    <input type="password" class="form-control" name="password2" id="password2" placeholder="Your password"
                           [(ngModel)]="model.password2">
                </div>
                <div class="form-group">
                    <textarea rows="4" cols="50" placeholder="Description" [(ngModel)]="model.description"]></textarea>
                </div>
                <div class="row">
                    <div class="col-xs-1">
                        <button value="update" type="button" class="btn btn-primary pull-left">Update</button>
                    </div>
                    <div class="col-xs-1">
                        <button value="cancel" type="button" class="btn btn-primary pull-left">Cancel</button>
                        <div class="clear"></div>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-outline-dark" (click)="activeModal.close('Close click')">Close</button>
</div>
Run Code Online (Sandbox Code Playgroud)

它工作正常,直到我添加到模型,完整的HTML在上面按要求.

我也无法正确并排获得按钮.

mic*_*yks 51

[(ngModel)]="model.description"] //']'被不必要地添加

改为

[(ngModel)]="model.description"

不要将其包裹在方括号中.

更新:

您可以model根据需要将变量初始化为:

model:ModalComponentModel=<ModalComponentModel>{};
Run Code Online (Sandbox Code Playgroud)

要么

model:ModalComponentModel=<ModalComponentModel>[];
Run Code Online (Sandbox Code Playgroud)


Saj*_*ran 8

问题在这条线

更改

  <textarea rows="4" cols="50" placeholder="Description" [(ngModel)]="model.description"]></textarea>
Run Code Online (Sandbox Code Playgroud)

  <textarea rows="4" cols="50" placeholder="Description" [(ngModel)]="model.description"></textarea>
Run Code Online (Sandbox Code Playgroud)


ver*_*ika 5

您在以下代码行中遇到问题:

<textarea rows="4" cols="50" placeholder="Description" [(ngModel)]="model.description"]></textarea>
Run Code Online (Sandbox Code Playgroud)

它应该是:

<textarea rows="4" cols="50" placeholder="Description" [(ngModel)]="model.description"></textarea>
Run Code Online (Sandbox Code Playgroud)

有一个额外的括号导致此错误。


Eli*_*Eli 5

就我而言,是的<app-some-component []></app-some-component>。空[]是导致问题的原因。


Sat*_*hia 5

就我而言,我没有]在 ngModel 中关闭。我使用了错误的代码。

[(ngModel)="selectedTestName"
Run Code Online (Sandbox Code Playgroud)

正确的语法:

[(ngModel)]="selectedTestName"
Run Code Online (Sandbox Code Playgroud)