2017-02-09 3 views
0

入力用のカスタムディレクティブを作成しました。これがtrueに$ scope.ngReadonlyを設定しますが、AngularJS:ng-readonly変数をtrue inside directiveに設定しても動作しません。

var myInputDirective = (function() { 
directives.directive('myInput', [function() { 
return { 
    restrict: 'E', 
    transclude: 'element', 
    replace: true, 
    require: 'ngModel', 
    templateUrl: 'app/templates/common/myInput.tpl.html', 
    scope: { 
    ngModel: '=', 
    ngReadonly: '=' 
    }, 

    link: function($scope, $element, $attr, ngModelController) { 

    $scope.$watch('ngModel', function(newValue, oldValue) { 
     if (newValue != oldValue && newValue !== undefined && newValue !== null) { 
     $scope.ngReadonly = isReadOnly($attr.ngModel); 
     } 
    }); 

しかし - フィールド:それはこのように使用されるHTML内 :ディレクティブmyInput.js内部の今 <my-input class="input-text" type="number" ng-model="modelVariable" ng-readonly="false" />

私はこのような何かを持っていますまだ読んでいないです - 私はなぜ非常に混乱しています。

答えて

0

ディレクティブのtempalteセットng-readonlyのビューを、親スコープと同じにするには、input要素の属性の1つとして追加する必要があります。

return { 
    //..the other properties 
    scope: { 
    ngModel: '=', 
    ngReadonly: '=readOnly' 
    } 

その後myInput.tpl.htmlであなたのinput elementを見つけ、これを追加します:

<input ng-readonly="readOnly">

ただ、このような

関連する問題