在angular指令中输入类型number会产生字符串

Input type number inside angular directive produces string

本文关键字:字符串 number 类型 angular 指令 输入      更新时间:2023-09-26

我有以下指令:

<!-- directive -->
angular.module('app', [])
  .controller('MainCtrl', function($scope) {})
  .directive('test', function() {
    return {
      restrict: 'E',
      link: function(scope) {
        scope.value = 0;
        scope.type = 'number';
      },
      templateUrl: 'template.html'
    }
  });
<!-- template -->
<label>{{ type }}: <!--type == 'number' -->
  <!-- produces strings -->
  <input type="{{type}}" ng-model="value" class="form-control"  />
  <!-- produces integers -->
  <input type="number"  ng-model="value" class="form-control" />
</label>
<div>{{ value |json }}</div>

<!-- usage -->
<test />

第一个输入框产生字符串,第二个输入框产生整数。在我看来,这是一个bug。有什么想法或解决方法吗?

这里有一个活塞来复制它:http://plnkr.co/edit/uLjYKxepzaaZIjGmObzi?p=preview

问候Lennart

这不一定是一个bug,但在某种程度上是预期的行为。这与浏览器兼容性有关。