0

Angular のインプット マスク プラグインに配列を渡すことができません。多分誰かがこれで私を助けることができます。

angular.module('myproject.directives').   
directive('inputMask', function() {
    return {
        restrict: 'A',
        scope: {
            inputMask: '@'
        },
        link: function(scope, el, attrs) {
            $(el).inputmask(attrs.inputMask);
        }
    };
});

<input type="text" input-mask="{'mask': '9{4} 9{4} 9{4} 9{4}[9]', 'autoUnmask': 'true'}" />
4

2 に答える 2

0

属性値は、プラグインに渡すために必要なオブジェクトではなく、文字列を返します

文字列が有効な JSON になるように引用符を切り替えてから、json をオブジェクトに解析できます。

<input type="text" input-mask='{"mask": "9{4} 9{4} 9{4} 9{4}[9]", "autoUnmask": "true"}' />

JS

.directive('inputMask', function() {
    return {
        restrict: 'A',
        scope: {
            inputMask: '@'
        },
        link: function(scope, el, attrs) {
          var mask =JSON.parse(attrs.inputMask);
           $(el).inputmask(mask);
        }
    };
})

しかし、現実的には、文字列を html に配置せず、代わりにコントローラーから分離スコープにオブジェクト参照を渡す方がはるかに簡単です。

于 2016-02-05T15:18:30.843 に答える
0

メソッドを使用して、属性scope.$evalで式を実行するだけです。inputMask

angular.module('myproject.directives')  
.directive('inputMask', function() {
    return {
        restrict: 'A',
        scope: {
            inputMask: '@'
        },
        link: function(scope, el, attrs) {
            $(el).inputmask(scope.$eval(attrs.inputMask));
        }
    };
});

<input type="text" input-mask="{'mask': '9{4} 9{4} 9{4} 9{4}[9]', 'autoUnmask': 'true'}" />
于 2016-02-05T15:31:36.563 に答える