Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这个字段在Angular2中没有定义?_Javascript_Angular - Fatal编程技术网

Javascript 为什么这个字段在Angular2中没有定义?

Javascript 为什么这个字段在Angular2中没有定义?,javascript,angular,Javascript,Angular,我有一个Angular2的模板: <tr *ngFor="#user of users"> <td> <label *ngIf="editing != user.id">{{ user.name }}</label> <input #username *ngIf="editing == user.id" value="{{ user.name }}" /> </td>

我有一个Angular2的模板:

<tr *ngFor="#user of users">
    <td>
        <label *ngIf="editing != user.id">{{ user.name }}</label>
        <input #username *ngIf="editing == user.id" value="{{ user.name }}" />
    </td>
    <td>
        <label *ngIf="editing != user.id">{{ user.lastname }}</label>
        <input #lastname *ngIf="editing == user.id" value="{{ user.lastname }}" />
    </td>
    <td>
        <label *ngIf="editing != user.id">{{ user.gender | genderPipe }}</label>
        <select #gender *ngIf="editing == user.id" value="{{ user.gender }}">
            <option value="0">
                {{ 0 | genderPipe }}
            </option>
            <option value="1">
                {{ 1 | genderPipe }}
            </option>
        </select>
    </td>
    <td>
        <img (click)="removeUser(user.id)" width="16px" class="pull-left" src="/images/close.png" />
        <img (click)="editUser(user.id)" *ngIf="editing != user.id" class="pull-left" src="/images/edit.png" />
        <img (click)="saveUser(user.id, username)" *ngIf="editing == user.id" class="pull-left" src="/images/save.png" />
    </td>

{{user.name}
{{user.lastname}
{{user.gender}genderPipe}
{{0 | genderPipe}}
{1 | genderPipe}
我将
#username
属性设置为第一个输入。 在
saveUser()
中,当我发送
username
参数时,它是
undefined

为什么呢?如何解决此问题?

您应该使用
[(ngModel)]
更新属于它的所有属性的值,而不是使用
属性和
{}
插值

标记

<tr *ngFor="#user of users">
    <td>
        <label *ngIf="editing != user.id">{{ user.name }}</label>
        <input *ngIf="editing == user.id" [(ngModel)]="user.name" />
    </td>
    .....
    <td>
        .....
        <img (click)="saveUser(user)" *ngIf="editing == user.id" class="pull-left" src="/images/save.png" />
    </td>

{{user.name}
.....
.....

因为“name”已经是输入的属性了吗?你能试着把名字改成别的名字吗,比如
username
?我试过了,它是一样的。可能是吗,因为它在iTation内?显然是psuedocode,但您在
saveUser()中使用的是
user.name
,而不是
username
对吗?@Pablo为什么不使用
ngModel
进行绑定?这就是为什么你必须像@PankajParkar那样使用ngModel,这样它会随着输入值user.name一起更新。在组件中,仍然有输入的原始值。我必须这样设置模型:
[(ngModel)]