【发布时间】:2013-06-07 04:27:02
【问题描述】:
我一直在尝试为我的 Angular 应用程序编写一个共享指令,将输入字段转换为 jquery timepicker。
我有两个输入字段,fromTime 和 toTime,我想在 fromTime 更改时更新 toTime。
我的代码如下:
HTML
<input type="text"
time-picker="{ timeFormat: 'H:i'}"
class="input-block-level"
name="fromTime"
ng-model="filter.fromTime"
ng-change="update()"
/>
<input type="text"
time-picker="{ timeFormat: 'H:i'}"
class="input-block-level"
name="toTime"
ng-model="filter.toTime"
ng-change="update()"
/>
指令
sharedServices.directive('TimePicker', function () {
return {
restrict: 'A',
scope: {
time: "=ngModel"
},
link: function(scope, element, attrs) {
//Initialize the timepicker
$(element).timepicker(scope.$eval(attrs.TimePicker));
//watch for changes
scope.$watch('time', function(stuff) {
console.log('some change', stuff);
});
}
};
});
包括共享服务
var roomsApp = angular.module("roomsApp", ["ngResource",'ui', 'sharedServices','ui.bootstrap']).config(function($routeProvider) { ...
指令加载并且时间选择器被初始化并附加到元素,但是当我更改输入字段时没有任何反应,甚至没有附加到元素的 ng-change 事件。页面加载时输入字段也是空的,即使模型设置为在应用程序的控制器中包含某些值。谁能解释一下这个问题?
*更新 http://plnkr.co/edit/t3BzShezEdh29ZAlI8ZI?p=preview 这说明了我的问题。更改日期时没有控制台记录任何内容
【问题讨论】:
-
向我们展示包含指令的代码。我看到了一些非常奇怪的事情。
-
@TheHippo ,完成。指令加载并且时间选择器被初始化。但是作用域和指令之间的绑定不能正常工作。
-
请继续利用 ngModelController 但不要作为隔离范围的道具,将其设置为作为 linkibg 函数的第四个参数注入的必需控制器,使用其 $viewValue 重载其 $render 方法并在您的 UI 小部件更改上使用其 $setViewValue 并结合在范围上调用 $apply。
标签: javascript html angularjs angularjs-directive