【问题标题】:How can I set the value of input-group spinner to value of 1?如何将输入组微调器的值设置为 1?
【发布时间】:2018-06-07 01:04:13
【问题描述】:

老实说,我认为这是一个很容易解决的问题,但我一直在努力解决这个问题。我想将我的输入组微调器设置为在用户打开应用程序时显示值 1。

<input id="option1" type="number" min="1" value ="1"
           class="form-control"
           ng-model="targetEntity.option1">
</div>

我认为 value="1" 可以解决问题,但它没有显示出预期的结果,我想知道我做错了什么?

【问题讨论】:

  • 什么微调器?您的意思是在输入字段中显示1 吗? (你可以通过ng-init="targetEntity.option1 = 1" 做到这一点)
  • 不要使用ng-init,在控制器中设置初始值。
  • ng-init 可以被滥用来在您的模板中添加不必要的逻辑量。 ngInit 只有少数合适的用法。您应该使用组件或控制器而不是 ngInit 来初始化作用域上的值。有关详细信息,请参阅AngularJS ngInit Directive API Reference
  • 我将如何处理@Lex,我确实尝试了 ng-init,但不幸的是它没有用。抱歉,刚接触这个。
  • 为什么不在控制器中声明 targetEntity.option1 的默认值?

标签: html angularjs twitter-bootstrap


【解决方案1】:

在你的控制器中初始化targetEntity.option1的值:

angular.module('app', [])
  .controller('ctrl', ($scope) => {
    $scope.targetEntity = {
      option1: 1
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <input id="option1" type="number" min="1"
         class="form-control"
         ng-model="targetEntity.option1" />
</div>

【讨论】:

  • 你明白了,这就是我要说的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多