有一些代码
app.directive('currencyInput', function ($filter, myFactory) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
ctrl.$formatters.unshift(function () {
return $filter('number')(ctrl.$modelValue);
});
ctrl.$parsers.unshift(function (viewValue) {
let plainNumber = viewValue.replace(/[\,\.]/g, ''),
b = $filter('number')(plainNumber);
elem.val(b);
//here I need to add listener if keyCode==13
return plainNumber;
});
}
};
});
所以,我需要抓住keyCode == 13。我怎么能在我添加评论的地方做到这一点?我需要采取一些行动:
elem.bind('keydown keypress', ($event) => {
if ($event.which === 13) {
let val=$element.val();
if(attrs['param']=="amount") myFactory.process[attrs['param']]=val*24;
else myFactory.process[attrs['param']]=$element.val();
let i=0;
for(let key in myFactory.process){
if(myFactory.process[key]===""){
scope.dashboard.currParam=i;//this doesn't works until user make one more action.
break;
}
i++;
}
}
})
但并非所有行动都是"行为"用户点击进入。 myFactory.process看起来像
process: {
cost:"",
amount:"",
wrapping:"",
risk:"",
limit:"",
franchise:""
},
此字符串scope.dashboard.currParam=i;
必须更新视图(DOM)。但是在我点击输入之前它不会更新。
答案 0 :(得分:0)
您可以使用以下代码执行此操作:
elem.bind('keydown keypress', ($event) => {
if ($event.which === 13) {
scope.$apply(() => scope.$eval(attrs.enter, { $event });
// enter key pressed here
}
});
答案 1 :(得分:0)
问题是焦点。所以,你需要做出类似的事情:
elem.bind('keydown keypress', ($event) => {
if ($event.which === 13) {
let val=$element.val();
if(attrs['param']=="amount") myFactory.process[attrs['param']]=val*24;
else myFactory.process[attrs['param']]=$element.val();
let i=0;
for(let key in myFactory.process){
if(myFactory.process[key]===""){
scope.dashboard.currParam=i;
let target = $event.target;
target.blur();//this helps
break;
}
i++;
}
}
})