简体   繁体   English

范围输入不会在模板驱动形式(角度2)中反映默认值

[英]Range Input won't reflect default value in Template Driven Form (Angular 2)

I want my range inputs to load with a default value of 0. When the page loads each input is set to the middle by default. 我希望我的范围输入加载时的默认值为0。默认情况下,在页面加载时,每个输入都设置为中间。 When I inspect the element I see this 当我检查元素时,我看到了

<input
    class="form-control ng-untouched ng-pristine ng-valid"
    step="1"
    type="range"
    ng-reflect-model=""
    name="my_own_job"
    id="bus_info_04_a_01_01"
    min="0"
    max="10"
    value="0"
>

All the way at the end you see the value is indeed passed in. Here's what the input looks like which is inside of an *ngFor by the way. 一直到最后,您确实看到了值的传递。这就是*ngFor内的输入外观。

<input class="form-control" type="range" step="1"
    [attr.name]="ans.ctrlName"
    [attr.id]="ans.id"
    [attr.min]="Data.loExtreme"
    [attr.max]="Data.hiExtreme"
    [attr.value]="ans.value"
    [(ngModel)]="UserResponse[ans.respName]"
    #{{ans.respName}}="ngModel"
>

I'm also creating my variables dynamically like this 我也像这样动态创建变量

//From parent component
@Input() Data : Question;

//For dynamic variables for ngModel
UserResponse : { [ propName : string ] : string } = {};

//Function called in OnInit
loadResponses(){

     let data       : any           = this.Data;
     let control    : string        = data.ctrlName;
     let response   : string        = data.respName;
     let multiResp  : Array<any>    = data.answers;
     let defVal     : string        = '';
     let load       : Array<string> = [];

     load.push( control );

     if( response == '' || response == undefined ){

         if( this.Template == 'multi_fader' ){

             multiResp.forEach( resp => {

                 this.UserResponse[ resp.ctrlName ] = resp.answer;
                 this.UserResponse[ resp.respName ] = 0;

             });

         }
         else {

             multiResp.forEach( resp => {

                 this.UserResponse[ resp.ctrlName ] = resp.answer;
                 this.UserResponse[ resp.respName ] = defVal;

            });
        }
     }
     else {

         load.push(response);
         load.forEach( ctrl => { this.UserResponse[ ctrl ] = defVal; });

         this.UserResponse[ this.Data.ctrlName ] = this.Data.question;

    }
}

I tried setting the value to UserResponse[respName] but the value property on the input just shows up as the word value with no equal next to it. 我尝试将值设置为UserResponse[respName]但输入的value属性只是显示为单词value ,其旁边没有相等。 I back and forth taking out the value property all together leaving just the ngModel stuff as well as commenting out the part of the function that sets the default value to see if just the value property alone would work without any other interference from from the function and it was the same result with the value being passed but the inputs being set in the middle. 我来回取出所有value属性,只留下ngModel内容,并注释了设置默认值的函数部分,以查看仅value属性是否可以正常工作而不会受到函数的任何其他干扰,传递值的结果相同,但输入设置在中间。 I tried setting it to 1 just to see if maybe 0 was making it null but that made no difference. 我尝试将其设置为1只是为了看看是否0使它为null但这没什么区别。

I even changed the value property on each question for that input in my json file to 1 and it shows up in the inspection yet doesn't change the default placement of the input. 我什至将json文件中该输入的每个问题的value属性更改为1 ,它在检查中显示,但没有更改输入的默认位置。 Can someone please shed some light on why this is happening and how to fix it? 有人可以说明为什么会发生这种情况以及如何解决吗? Here's a Plunkr of the entire mechanism if you need to see what else is going on. 这里有一个Plunkr整个机制的,如果你需要看看还有什么是怎么回事。

If you change in the template, 如果您更改模板,

[attr.value]="ans.value"

to, 至,

[(ngModel)]="ans.value"

it should initialize the value to zero in the range input view (confirmed in plunker). 它应该在范围输入视图中将其值初始化为零(已在plunker中确认)。 Not sure what you are doing with the other ngModel in your template though. 虽然ngModel模板中的另一个ngModel在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM