LoginSignup
0
1

More than 3 years have passed since last update.

Angularjsで input type="number" を使うとコンソール側でエラーがでるやつ

Last updated at Posted at 2016-05-31

angularを使ったformで<input type="number">フォーマットを使うとき

引用: https://docs.angularjs.org/error/ngModel/numfmt

ディレクティブの作成

controllers/directive/stringToNumber.js
/**
 *  controllers/directive/stringToNumber.js
 */

;(function() {
  'use strict';

angular.module('webApp')
  .directive('stringToNumber', function() {
    return {
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        ngModel.$parsers.push(function(value) {
          return '' + value;
        });
        ngModel.$formatters.push(function(value) {
          return parseFloat(value, 10);
        });
      }
    };
  });

})();

使い方

index.html
<input type="number" string-to-number ng-model="account_number">
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1