0

ディレクティブの ng-model を介してコントローラー スコープから値を渡す独自のモデルを持つテキスト入力を含む AngularJS ディレクティブがあります。

このペン (および以下のコード) をチェックしてください: http://codepen.io/ericwshea/pen/KwXRyr

問題は、そのモデルがたまたま null または未定義の値になる場合があることです。その場合、テキスト入力の ngModelController を使用して、テキスト入力の null 値の表示を「NULL」のようにフォーマットします。

値がフォーマッタで一致する任意の文字列である場合は機能しますが、値が null の場合は機能しません (未定義の同じ結果でもテストしました)。

これに関する洞察/回避策、またはこれは単なる $formatters の欠点ですか?

HTML:

<div ng-app="app" class="container">
  <div ng-controller="ctrl" class="col-md-12">
    <form>
      <input-directive ng-model="model"></input-directive>
      <input-directive ng-model="model2"></input-directive>
      <div ng-if="model">Model 1: {{model}}</div>
      <div ng-if="model2">Model 2: {{model2}}</div>
    </form>
  </div>
</div>

ジャバスクリプト:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.model = null;
    $scope.model2 = 'make this null';
  })

  .directive('inputDirective', function() {
    var template = 
        '<div>'+
          '<div class="input-group">'+
            '<input type="text" class="form-control" ng-model="localModel">'+
            '<span class="input-group-btn">'+
              '<button ng-click="save()" class="btn btn-default" type="button">Save</button>'+
            '</span>'+
          '</div>'+
        '</div>';

    function link (scope, elem, attr) {
      var inputModelCtrl = elem.find('input').controller('ngModel');

      function formatter(val) {
        if (val === 'make this null') {
          return scope.nullValue;
        }
        if (val === null) {
          return scope.nullValue;
        }
        return val;
      }

      scope.nullValue = 'NULL';
      scope.localModel = scope.ngModel;
      scope.save = function() {
        scope.ngModel = scope.localModel;
      }
      inputModelCtrl.$formatters.push(formatter);
    }

    return {
      restrict: 'E', 
      replace: true,
      require: 'ngModel',
      template: template,
      link: link,
      scope: {
        ngModel: '='
      }
    }
  })
;
4

1 に答える 1

0

私はこれに対する回避策を見つけることができました。ngModel にウォッチを配置し、null をキャッチして「NULL」にすることで、ディレクティブが作成されるときに値を初期化します。これは、angular $formatters が実際にフォーマットします。localModel のビュー値を にフォーマットすると、機能します。

'NULL' をディレクティブからコントローラ スコープに送り返すときにモデル値が正しいことを確認するには、ngModelController で $parser を使用して 'NULL' を監視し、それを null に変換します。

完全な円にするために、ngModel のディレクティブ内の $watch は、ディレクティブの外で値が null に設定されている場合、localModel が「NULL」に再初期化され、適切な表示用にフォーマットされるようにします。

これはばかげているように思えるかもしれませんが、実際のディレクティブは、ご想像のとおり、この機能を必要とするカスタム選択メニューです。

ここで変更を確認できます: http://codepen.io/ericwshea/pen/KwXRyr

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.model = null;
    $scope.getModel = function() {
      return $scope.model === null ? 'null' : $scope.model;
    }
    $scope.setToNull = function() {
      $scope.model = null;
    }
  })

  .directive('inputDirective', function() {
    var template = 
        '<div>'+
          '<div class="input-group">'+
            '<input type="text" class="form-control" ng-model="localModel">'+
            '<span class="input-group-btn">'+
              '<button ng-click="save()" class="btn btn-default" type="button">Save</button>'+
            '</span>'+
          '</div>'+
        '</div>';

    function link (scope, elem, attr, ngModelCtrl) {
      var inputModelCtrl = elem.find('input').controller('ngModel');
      var nullDisplay = '<NULL>';

      function ngModelParser(val) {
        if (val === 'NULL') return null;
        return val;
      }

      function localModelFormatter(val) {
        if (val === 'NULL') return nullDisplay;
        return val;
      }

      scope.save = function() {
        ngModelCtrl.$setViewValue(scope.localModel);
        console.log(ngModelCtrl.$modelValue);
      }

      scope.$watch('ngModel', function(val) {
        if (val === null) val = 'NULL';
        scope.localModel = val;
      })

      inputModelCtrl.$formatters.push(localModelFormatter);
      ngModelCtrl.$parsers.push(ngModelParser);
    }

    return {
      restrict: 'E', 
      replace: true,
      require: 'ngModel',
      template: template,
      link: link,
      scope: {
        ngModel: '='
      }
    }
  })
;

このシナリオはかなり具体的ですが(自分で答えなければならないほど具体的です!)、これが同様の問題に遭遇した人に役立つことを願っています。

于 2015-02-19T20:20:29.087 に答える