Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.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 设置ngModel默认值angular 2_Javascript_Angular_Angular2 Forms_Angular2 Ngmodel_Ngmodel - Fatal编程技术网

Javascript 设置ngModel默认值angular 2

Javascript 设置ngModel默认值angular 2,javascript,angular,angular2-forms,angular2-ngmodel,ngmodel,Javascript,Angular,Angular2 Forms,Angular2 Ngmodel,Ngmodel,angular 2中的ngModel有问题。有一个任务从组件类中的数组输出两个输入。 发现了一种可能性,可以使ngModel从[name]属性获取其值,而不在[()]中包含ngModel。我想知道是否有办法为这些输入提供默认值 personal-details.component.ts: import { Component, } from '@angular/core'; import { Input }from './Input' @Component({ select

angular 2中的ngModel有问题。有一个任务从组件类中的数组输出两个输入。 发现了一种可能性,可以使ngModel从[name]属性获取其值,而不在[()]中包含ngModel。我想知道是否有办法为这些输入提供默认值

personal-details.component.ts:

import {
    Component,
} from '@angular/core';

import { Input }from './Input'

@Component({
     selector: 'personal-details',
     styleUrls: ['personal-details.component.sass'],
     templateUrl: 'personal-details.component.html'
})
export class PersonalDetailsComponent {
     title: string;
     inputs: Input[] = [
         new Input('Name', 'text', 'Name', true, true),
         new Input('Surname', 'text', 'Surname', true, true),
         new Input('Mobile phone Number', 'text', 'phone', true, true),
         new Input('Date of Birth', 'text', 'birthday', false, true),
         new Input('Title', 'text', 'title', false, false),
         new Input('Title after name', 'text', 'title-after-name', false,     false),
         new Input('Personal number', 'text', 'personal-number', false, false),
         new Input('National ID/Passport number', 'text', 'personal-id', false, true),
    ];
    save = function (form) {
        console.log(form);
    }
    constructor(){
        this.title = 'someName';
    }
}
这是我的模板:

<h4 class="profile__title">Personal Details</h4>
<form #personalDetails="ngForm"   (ngSubmit)="save(personalDetails.value)">
    <div layout="row" flex-wrap>
         <div class="profile__input-wrapper" *ngFor="let input of inputs" flex="33">
             <md-input-container class="profile__input-container">
                <input md-input
                   [placeholder]="input.placeholder"
                   [type]="input.type"
                   [name]="input.name"
                   [disabled]="input.isDisabled"
                   [required]="input.isRequired"
                   ngModel>
            </md-input-container>
        </div>
    </div>
    <profile-footer ></profile-footer>
</form>
个人详细信息

尝试了几种其他方法使用ngFor列出它们,但没有成功。

它应该像绑定到value属性一样简单:

[value]="input.intitialValue"
或者如果这不起作用:

[ngValue]="input.intitialValue"

通过添加ngModel=“{input.defaultValue}}”解决了该问题。

简单的方法是使用带有单向绑定的ngModel

<input md-input
    [placeholder]="input.placeholder"
    [type]="input.type"
    [name]="input.name"
    [disabled]="input.isDisabled"
    [required]="input.isRequired"
    [ngModel]="input.value">


它会将初始值传递到输入,而不会对事件做出反应,也不会将更改传递回模型。

谢谢您的回答![ngValue]返回语法错误。[值]似乎有效,但在实际视图中未显示结果。虽然ng reflect value=“1”出现在DOM中。噢,糟了!我忽略了,您是在单独进行ngModel的单向绑定(当您进行ngModel时)。因此,您将其更改为双向数据绑定,这是可行的。对我来说,我是个失败者。