在另一个元素的输入中激活指令 - AngularJS

时间:2016-10-13 09:39:15

标签: html angularjs angularjs-directive

在以下范围中,我有一个日历图标和一个输入字段。但是,我要做的是在用户点击日历图标时触发jqdatepicker指令。但是如果我按照预期在任何其他元素中调用该指令,它将触发日历显示,但是当选择日期时,输入字段将不会更新。有什么方法可以解决这个问题吗? 这就是我的html的样子:

<span class="input-wrapper">
  <i class="icon calendar"></i>
  <input  data-ng-model="finish" type="text" jqdatepicker data-ng-blur="update()">
</span>

指令:

.directive('jqdatetimepicker', function() {
    return {
      restrict: 'A',
      link: function (scope, element) {
        element.datetimepicker({
          lang:'de',
          i18n:{
            en:{
               months: [
                "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
              ],
              dayOfWeek: [
                "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
              ]
             }
          },
          timepicker:true,
          format:'d.m.Y H:i'
        });
      }
    };
  })

1 个答案:

答案 0 :(得分:0)

字段未更新的原因是因为jquery未与angular同步。

我不确定您正在使用哪个jquery插件,但通常他们会公开//this is pseudo code of course, but the idea would be same for any method onSelect: function(data){ scope.finish = data; //this is how you tell angular that somthing changed and it needs to run the digest cycle scope.$apply(); } 事件(或某种类型的事件),您可以使用它来更新输入:

componentDidMount