javascript - AngularJs - 将一个 ng-model 绑定(bind)到具有两个输入的指令

标签 javascript angularjs angularjs-filter angular-directive

如何创建 range绑定(bind)到一个 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) {

        }
    };
})

最佳答案

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

作为练习,我在这个 fiddle 中实现了它: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>

我怀疑过滤器不会带你到任何地方,因为它们不进行双向绑定(bind)。


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

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

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

还有一个概念证明:http://jsfiddle.net/W99rX/

关于javascript - AngularJs - 将一个 ng-model 绑定(bind)到具有两个输入的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25141773/

相关文章:

angularjs - typescript 抛出一个属性在类型上不存在,即使包含类型文件

angularjs - 用于从 api rest 获取的存储数据的 Angular 提供程序(服务)?

javascript - ECMAScript6 AngularJS 过滤器

javascript - 同一页面上的多个过滤器 - 搜索、过滤、排序

javascript - 我如何从名称值中收集所有数据以便我可以使用它?

javascript - angularJS UI选项卡打印选项卡中选定的内容

angularjs - 如何掩盖UI-Tinymce文本区域的边界

javascript - 极限阵列, Angular

javascript - 如果选择下拉菜单,则显示额外的表单字段

javascript - 如何删除除被单击元素之外的所有其他元素上的类