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

Hig*_*don 2 javascript angularjs angularjs-filter angular-directive

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

我有这个Html:

<div tu-range ng-model="arbitraymodel" />
Run Code Online (Sandbox Code Playgroud)

一个模型:

var arbitrarymodel = "10/22";
Run Code Online (Sandbox Code Playgroud)

边注; 我创建了一个过滤器来分割这两个值:
{{ 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) {

        }
    };
})
Run Code Online (Sandbox Code Playgroud)

Nik*_*los 8

正确的方法(IMO)是创建这里描述的自定义控件.

作为练习,我在这个小提琴中实现它: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);
            });
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

它的用法是:

<range ng-model="ctrl.theRange" name="myRange" required="true"></range>
Run Code Online (Sandbox Code Playgroud)

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


编辑:即使这解决了问题,我建议采用略有不同的方法.我会将range指令的模型定义为一个对象:

{
    from: ...,
    to:   ...
}
Run Code Online (Sandbox Code Playgroud)

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

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