AngularJs - 绑定一条NG-模型两个输入指令 [英] AngularJs - bind one ng-model to directive with two inputs
问题描述
如何创建结合一个 NG-模型
和输出两个范围
指令>使用滤波器的输入
字段(已经取得)。基本上我有一个方向的从模型到输入的工作,但对方的从输入到的模型没有。如何实现这一目标?
我有这个网站:
< DIV TU-NG范围模型=arbitraymodel/>
和一个模型:
VAR arbitrarymodel =10/22;
旁注;我创建了一个过滤器来分割这两个值:
{{feature.Value |驳:\\'/ \\':0}}
和这个指令:
.directive('tuRange',函数($编译){
返回{
限制:'A',
要求:'ngModel',
范围: {
特点:'=',
tudisabled:'=',
型号:'= ngModel'//编辑
},
模板:'<输入类型=文本+
'NG值={{型号|分裂:\\'/ \\':0}}/>'+ //编辑,以模式
' - <输入类型=文本+
'NG值={{型号|分裂:\\/ \\:1}}/>',//编辑以模式
链接:功能(范围,元素,属性,ngModel){ }
};
})
的正确方法(IMO)是创建一个自定义的控制所描述的这里。
作为练习,我实现了它在此琴: http://jsfiddle.net/6cn7y/
该指令的code是(你可能需要调整一些细节):
app.directive(范围功能(){
变种的ID = 0; 功能constructRangeString(从,到){
VAR的结果;
如果(从&安培;!&安培;!到){
结果= NULL;
}
否则,如果(从){
结果=从+/+(以||);
}
否则,如果(到){
结果=/+到;
}
返回结果;
} 返回{
要求:ngModel
限制:E,
更换:真实,
范围: {
名称: @
},
模板:
'< DIV NG型={{subFormName}}>' +
'<输入类型=文本NG模型=从级=范围 - 从/>' +
'<输入类型=文本NG-模式=到阶级=系列到/>' +
'< / DIV>,
链接:功能(范围,ELEM,ATTRS,ngModel){
VAR重新= /([0-9] +)\\ /([0-9] +)/; 如果(scope.name){
scope.subFormName = scope.name;
}
其他{
scope.subFormName =_range+ ID;
ID ++;
} ngModel。$ =渲染功能(){
VAR的结果,从到;
结果= re.exec(ngModel $ viewValue);
如果(结果){
从= parseInt函数(结果[1]);
为= parseInt函数(导致[2]);
}
scope.from =距离;
scope.to =到;
}; 范围。$表(从功能(的newval){
VAR的结果= constructRangeString(的newval,scope.to);
ngModel $ setViewValue(结果)。
});
范围。$表(到功能(的newval){
VAR的结果= constructRangeString(scope.from,的newval);
ngModel $ setViewValue(结果)。
});
}
};
});
和它的用法是:
<范围NG-模式=ctrl.theRangeNAME =myRange所需=真>< /范围和GT;
我怀疑过滤器将让你与此任何地方,因为他们没有做2路绑定。
修改:尽管这解决了这个问题,我建议稍微不同的方法。我将定义的模型中的范围
指令作为对象:
{
来源:...
至: ...
}
这意味着,在 ctrl.theRange输出
在示例变量将是像上述的目的。如果你真的想要的字符串格式从/到
,添加在 ngModel
管道解析器/格式化程序,即在 constructRangeString()
功能。使用解析器/格式化,在 ctrl.theRange
变量获得所需的字符串格式,同时保持code的模块化(即 constructRangeString()
的功能是外部的指令)和更多的参数(该模型是可以easilly被处理和转换格式)。
和概念证明: 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-模型两个输入指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!