AngularJs - 将一个ng-model绑定到带有两个输入的指令

时间:2014-08-05 14:47:32

标签: javascript angularjs angularjs-filter angular-directive

如何使用range(已制作)创建绑定到一个ng-model并输出两个input字段的filter指令。基本上我有一个方向从模型到输入工作,但另一方从输入到模型没有。怎么做到这一点?

我有这个Html:

<div tu-range ng-model="arbitraymodel" />

一个模特:

var arbitrarymodel = "10/22";

旁注;我创建了一个过滤器来分割这两个值:
{{ feature.Value | split:\'/\':0}}

这条指令:

.directive('tuRange', function($compile) {
    return { 
        restrict: 'A',
        require: 'ngModel',
        scope: {
            feature: '=',
            tudisabled: '=',
            model: '=ngModel' // edited
        },
        template: '<input type="text" '+
               'ng-value="{{ model | split:\'/\':0}}" />'+ // edited to 'model'
             '-<input type="text" '+
               'ng-value="{{ model | split:\'/\':1}}" />', // edited to 'model'
        link: function(scope, element, attributes, ngModel) {

        }
    };
})

1 个答案:

答案 0 :(得分:7)

正确的方法(IMO)是按照here所述创建自定义控件。

作为练习,我在这个小提琴中实现了它:http://jsfiddle.net/6cn7y/

该指令的代码是(您可能需要调整一些细节):

app.directive("range", function() {
    var ID=0;

    function constructRangeString(from, to) {
        var result;
        if( !from && !to ) {
            result = null;
        }
        else if( from ) {
            result = from + "/" + (to || "");
        }
        else if( to ) {
            result = "/" + to;
        }
        return result;
    }

    return {
        require: "ngModel",
        restrict: "E",
        replace: true,
        scope: {
            name: "@"
        },
        template:
            '<div ng-form="{{ subFormName }}">' +
                '<input type="text" ng-model="from" class="range-from" />' +
                '<input type="text" ng-model="to" class="range-to" />' +
            '</div>',
        link: function(scope,elem,attrs,ngModel) {
            var re = /([0-9]+)\/([0-9]+)/;

            if( scope.name ) {
                scope.subFormName = scope.name;
            }
            else {
                scope.subFormName = "_range" + ID;
                ID++;
            }

            ngModel.$render = function() {
                var result, from, to;
                result = re.exec(ngModel.$viewValue);
                if( result ) {
                    from = parseInt(result[1]);
                    to = parseInt(result[2]);
                }
                scope.from = from;
                scope.to = to;
            };

            scope.$watch("from", function(newval) {
                var result = constructRangeString(newval, scope.to);
                ngModel.$setViewValue(result);
            });
            scope.$watch("to", function(newval) {
                var result = constructRangeString(scope.from, newval);
                ngModel.$setViewValue(result);
            });
        }
    };
});

它的用法是:

<range ng-model="ctrl.theRange" name="myRange" required="true"></range>

我怀疑过滤器会让你随处可见,因为它们不会进行双向绑定。


编辑:尽管这解决了这个问题,但我建议采用略有不同的方法。我将range指令的模型定义为对象:

{
    from: ...,
    to:   ...
}

这意味着示例中ctrl.theRange变量的输出将是上述对象的输出。如果您真的需要字符串格式"from/to",请在ngModel管道中添加解析器/格式化程序,即constructRangeString()函数。使用解析器/格式化程序,ctrl.theRange变量获得所需的字符串格式,同时保持代码更加模块化(constructRangeString()函数在指令外部)并且更加参数化(模型的格式为可以很容易地进行处理和转换。)

概念证明:http://jsfiddle.net/W99rX/

相关问题