简体   繁体   English

Angular.js $ compile不起作用

[英]Angular.js $compile doesn`t working

I try to change all input type dates in project to uib-picker-popup, I use directive and $compile but new element doesn`t work. 我试图将项目中的所有输入类型日期更改为uib-picker-popup,我使用指令和$ compile,但是新元素不起作用。 When I put result html to new .html its working fine, I guess there is some problem with $compile. 当我将结果html放到新的.html时,我认为$ compile存在一些问题。

Source html 源HTML

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

Directive 指示

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));
        };
      }
    };
  });

Result html 结果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>

plunker here 这里的子

Thanks for help! 感谢帮助!

Just wrap it and it works: 只需包裹它,它的工作原理是:

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 http://plnkr.co/edit/JPSpVyWgDJeC0NtNhnib?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM