【问题标题】:Trouble getting a custom attribute from a directive从指令中获取自定义属性时遇到问题
【发布时间】:2017-09-13 09:40:37
【问题描述】:

我正在尝试编写一个指令,将默认日期设置为某个 datePicker。

只要我写了我的情况如下:

<input type="text" class="form-control" telme="insSupplies[$index].start_date" ng-model="insSupplies[$index].start_date" beat-date>

如您所见,我有一些带有 telme 自定义属性的 html,其中包含必须在 datePicker 中可见的默认日期

这是我的指令:

.directive('beatDate', function (DISPATCHER_BROADCAST) {
        return {
          restrict: 'A',
          require: "ngModel",
          link : function(scope, elem, attrs,ngModelCtrl) {
              scope.$on(DISPATCHER_BROADCAST.beatStuffLoaded,
                function(){ 
                  var parent = $(elem).parent();
                  var something = scope.$eval(attrs.telme);
                    var dtp = parent.datetimepicker({
                        format: "DD/MM/YYYY HH:mm:ss",
                        showTodayButton: false,
                        defaultDate: something.toDate()
                    });
                    dtp.on("dp.change", function (e) {
                        ngModelCtrl.$setViewValue(moment(e.date).format("DD/MM/YYYY HH:mm:ss"));
                        scope.$apply();
                    });
                }
              );        
          }
        };
      }) 

这里的问题是我无法以任何方式访问 attr telme

有什么提示吗?

【问题讨论】:

  • 如果您尝试访问telme 但超出scope.$on(...) 会发生什么情况?

标签: angularjs angularjs-directive datepicker attributes


【解决方案1】:

您是否尝试过执行“console.log(attrs)”以便查看哪些属性真正具有您的元素?

【讨论】:

  • 好吧,让我们做一件事。尝试将“attrs”存储到“$scope.on”行之前的某个变量中(例如“var myAttributes = attrs”)。然后执行“myAttributes”的“console.log”。我认为当“beatStuffLoaded”事件被触发时,你会失去“attrs”的值,所以存储它应该可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2012-09-04
  • 2012-06-06
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多