为什么下拉列表没有为ng-model的未定义值设置默认值?

时间:2017-06-14 17:22:13

标签: javascript angularjs

我想将下拉列表的默认值设置为1(现在它没有定义。 我尝试了下面的代码,但它没有用。

我尝试打印item.n_fold的值,它在页面中显示为空白。 我想将它设置为1,表示空值,空值或未定义值。

<div flex=5> <md-input-container>
{{item.n_fold}}
<input type="number" step="1" min="1" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? '': event.charCode >= 48 && event.charCode <= 57" id="train_n_file" ng-init="item.n_fold = item.n_fold || '1'" ng-model="item.n_fold" ng-disabled="item.train_select=='training_time_range'" style="margin-top: -2px;" class="input-div" value="<span id='hourpost'></span>
,or
<span id='daypost'></span>

<script>
var postTime = new Date('2017/6/9 00:01'); 
var now = new Date();
var difference = now.getTime() - postTime.getTime();
var minutes = Math.round(difference/60000);
var hours = Math.round(minutes/60);
var days = Math.round(hours/24);

var result;
if (days < 1) {
result = "Today";
} else if (days < 2) {
result = "Yesterday";
} else {
result = days + " Days ago";
}

document.getElementById("hourpost").innerHTML = hours + "Hours Ago" ;
document.getElementById("daypost").innerHTML = result ;
</script>
"> </md-input-container>
</div>

1 个答案:

答案 0 :(得分:0)

你写错了:

g-init =而不是 ng-init =

所以启动下拉列表的代码应该如下

<div flex=5> <md-input-container>
    {{item.n_fold}}
    <input type="number" step="1" min="1" 
        onkeypress="return (event.charCode == 8 || event.charCode == 0) ? '': event.charCode >= 48 && event.charCode <= 57" 
    id="train_n_file" 
    ng-init="item.n_fold= (item.n_fold || '1')" 
    ng-model="item.n_fold" 
    ng-disabled="item.train_select=='training_time_range'" 
    style="margin-top: -2px;" class="input-div" value="width"> 
    </md-input-container>
</div>