我为 jQuery UI 日期选择器创建了一个角度指令。问题是,当选择日期时,指令不会更新输入的 ng-model。知道为什么吗?
http://jsbin.com/ufoqan/1/edit http://jsbin.com/ufoqan/1/edit
AngularJS 实际上提供了一个特殊的控制器来与ngModel
您可以在指令中使用它;只需添加require: 'ngModel'
到你的指令定义。
这为您提供了第四个参数link
函数,这是您在中要求的控制器require
--在本例中,是一个实例of ngModelController http://docs.angularjs.org/api/ng.directive%3angModel.NgModelController。它有一个方法叫做$setViewValue
您可以使用设置模型的值:
app.directive('datepicker', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$(el).datepicker({
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
}
};
});
美丽的事情是关于ngModelController
它会自动处理验证和格式化(在特定输入的情况下)type
)以及与诸如此类的集成ngChange
回调;查看这个例子 http://jsbin.com/ufoqan/6/edit: http://jsbin.com/ufoqan/6/edit http://jsbin.com/ufoqan/6/edit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)