AngularJs - 将一个 ng-model 绑定到具有两个输入的指令 [英] AngularJs - bind one ng-model to directive with two inputs
问题描述
如何创建一个 range
指令,该指令绑定到一个 ng-model
并使用 filter输出两个
input
字段代码>(已经制作).基本上我有一个方向从模型到输入工作,但另一方面从输入到模型没有.如何实现这一目标?
我有这个 Html:
还有一个模型:
var任意模型=10/22";
旁注;我创建了一个过滤器来拆分这两个值:{{ feature.Value |拆分:\'/\':0}}
还有这个指令:
.directive('tuRange', function($compile) {返回 {限制:'A',要求:'ngModel',范围: {特征:=",tudisabled: '=',模型:'=ngModel'//已编辑},模板:'<input type="text" '+'ng-value="{{ model | split:\'/\':0}}"/>'+//编辑为 'model''-<输入类型="文本" '+'ng-value="{{ model | split:\'/\':1}}"/>',//编辑为 'model'链接:函数(范围、元素、属性、ngModel){}};})
正确的方法 (IMO) 是创建自定义控件,如此处.
作为练习,我在这个 fiddle 中实现了它:http://jsfiddle.net/6cn7y/
指令的代码是(你可能需要修改一些细节):
app.directive("range", function() {变量 ID=0;函数constructRangeString(从,到){变量结果;if( !from && !to ) {结果 = 空;}否则如果(来自){结果 = from + "/" + (to || "");}否则如果(到){结果=/"+到;}返回结果;}返回 {要求:ngModel",限制:E",替换:真的,范围: {姓名: "@"},模板:'<div ng-form="{{ subFormName }}">'+'<input type="text" ng-model="from" class="range-from"/>'+'<input type="text" ng-model="to" class="range-to"/>'+'</div>',链接:功能(范围,元素,属性,ngModel){var re =/([0-9]+)\/([0-9]+)/;如果(范围.名称){scope.subFormName = scope.name;}别的 {scope.subFormName = "_range" + ID;身份证++;}ngModel.$render = function() {var 结果,从,到;结果 = re.exec(ngModel.$viewValue);如果(结果){from = parseInt(result[1]);to = parseInt(result[2]);}scope.from = from;scope.to = 到;};scope.$watch("from", function(newval) {var 结果=constructRangeString(newval, scope.to);ngModel.$setViewValue(result);});scope.$watch("to", function(newval) {var 结果=constructRangeString(scope.from, newval);ngModel.$setViewValue(result);});}};});
它的用法是:
<小时>
我怀疑过滤器会让你在任何地方使用它,因为它们不进行双向绑定.
<小时>EDIT:即使这解决了问题,我还是建议采用稍微不同的方法.我会将 range
指令的模型定义为一个对象:
<代码>{从: ...,到: ...}
这意味着示例中 ctrl.theRange
变量中的输出将是一个与上述类似的对象.如果您真的想要字符串格式 "from/to"
,请在 ngModel
管道中添加解析器/格式化程序,即 constructRangeString()
函数.使用解析器/格式化程序,ctrl.theRange
变量获得所需的字符串格式,同时保持代码更加模块化(constructRangeString()
函数在指令外部)和参数化程度更高(模型采用易于处理和转换的格式).
还有一个概念证明:http://jsfiddle.net/W99rX/
How to create a range
directive which binds to one ng-model
and outputs two input
fields using a filter
(already made). Essentially I've got one direction from model to input working, but the other side from input to model not. How to achieve this?
I've got this Html:
<div tu-range ng-model="arbitraymodel" />
And a model:
var arbitrarymodel = "10/22";
Side note; I created a filter to split those two values:
{{ feature.Value | split:\'/\':0}}
And this directive:
.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) {
}
};
})
The correct way (IMO) is to create a custom control as described here.
As an exercise, I implemented it in this fiddle: http://jsfiddle.net/6cn7y/
The code of the directive is (you may need to adapt some details):
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);
});
}
};
});
And its usage would be:
<range ng-model="ctrl.theRange" name="myRange" required="true"></range>
I doubt the filters will get you anywhere with this, as they do not do 2-way binding.
EDIT: Even though this solves the problem, I would suggest a slightly different approach. I would define the model of the range
directive as an object:
{
from: ...,
to: ...
}
This means that the output in the ctrl.theRange
variable in the example would be an object like that of the above. If you really want the string format "from/to"
, add a parser/formatter in the ngModel
pipelines, i.e. the constructRangeString()
function. Using the parser/formatter, the ctrl.theRange
variable gets the desired string format, while keeping the code more modularized (the constructRangeString()
function is external to the directive) and more parameterized (the model is in a format that can easilly be processed and transformed).
And a proof of concept: http://jsfiddle.net/W99rX/
这篇关于AngularJs - 将一个 ng-model 绑定到具有两个输入的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!