0

我尝试将项目中的所有输入类型日期更改为 uib-picker-popup,我使用指令和 $compile 但新元素不起作用。当我将结果 html 放入新的 .html 时,它工作正常,我猜 $compile 存在一些问题。

源代码

<input type="date" ng-model="picker" />

指示

angular.module('ui.bootstrap.demo').directive('input', function ($compile) {
    return {
      require: 'ngModel',
      compile: function (tElem, tAttr) {
        if (tAttr.type !== 'date') {
          return;
        }

        return function (scope, elm, attrs, ngModelCtrl) {
            var ngModel   = elm.attr('ng-model');
            var name      = elm.attr('name');
            var className = elm.attr('class');
            var newElement = '<input type="text" class="' + className + '" ng-model="' + ngModel + '" name="' + name + '" uib-datepicker-popup is-open="isOpen">' +
                '<button type="button" class="btn btn-primary" ng-click="isOpen = true">+</button>';
            elm.replaceWith($compile(newElement)(scope));
        };
      }
    };
  });

结果html

<input type="text" class="ng-pristine ng-valid ng-scope ng-isolate-scope ng-valid-date ng-touched" ng-model="picker" name="undefined" uib-datepicker-popup="" is-open="isOpen" style="">

<button type="button" class="btn btn-primary ng-scope" ng-click="isOpen = true">+</button>

笨蛋在这里

感谢帮助!

4

1 回答 1

1

只需包装它就可以了:

var newElement = '<div><input type="text" class="' + className + '" ng-model="' + ngModel + '" name="' + name + '" uib-datepicker-popup is-open="isOpen">' +
                '<button type="button" class="btn btn-primary" ng-click="isOpen = true">+</button></div>';
elm.replaceWith($compile(newElement)(scope));

http://plnkr.co/edit/JPSpVyWgDJeC0NtNhnib?p=preview

于 2016-02-20T09:00:01.103 回答