【发布时间】:2015-12-24 08:48:33
【问题描述】:
当我按下任何键盘键时,例如0,如果我失去焦点,它会自动设置为 00:00,但它不会更新模型值。
angular.module('test').directive('timePicker', [function () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
model: '=ngModel'
},
link: function ($scope, element, attrs, ngModelCtrl) {
element.timepicker({'timeFormat' : 'H:i'});
////element.on('change', function () {
//// scope.$apply(function () {
//// scope.model = element.datepicker('getTime');
//// });
////});
////$scope.$watch('model', function (newValues, oldValues, scope) {
//// console.log("Nothing here");
////});
}
}
}]);
<input id="startTime" type="text" name="startTime" data-ng-model="vm.data.StartTime" time-picker />
由于模型未更新,我无法验证时间。 注释代码是我尝试更新的值。
关于如何解决这个问题的任何想法?
【问题讨论】:
-
您是否正确定义了模块,我可以看到您使用了模块语法的getter。
-
是的,当我从下拉列表中选择一个值时它正在工作,唯一的问题是它自动设置值时模型值没有更新。
-
我认为你做对了,元素的绑定发生了变化,尽管你应该使用“$scope.$apply()”(前面有一个 $ 就像你传递它一样在函数中)而不是“scope.$apply()”。看看angular-ui.github.io/bootstrap/#/timepicker :)
-
我无法访问 $scope 或更改事件中的范围。
-
你可以在这里找到更好的解释stackoverflow.com/a/29194068/2435473
标签: jquery angularjs angularjs-directive