Angularjs 如何使用自定义编译指令进行$compile

Angularjs 如何使用自定义编译指令进行$compile,angularjs,angularjs-directive,fullcalendar,angularjs-compile,Angularjs,Angularjs Directive,Fullcalendar,Angularjs Compile,有以下两条指令时,拖动并编译: 德拉格姆 路过后的平均数 var compiled = $compile('<div class="left content" compile="book.contents.name" id="book_{{book.id}}"></div>')(scope); 当我尝试var compiled=$compile(“”)(范围)关于dragMe指令的链接功能 它被发送到compile指令,但我无法填充已编译的div 编译了book.con

有以下两条指令时,拖动并编译:

德拉格姆 路过后的平均数

var compiled = $compile('<div class="left content" compile="book.contents.name" id="book_{{book.id}}"></div>')(scope);
当我尝试
var compiled=$compile(“”)(范围)关于dragMe指令的链接功能
它被发送到compile指令,但我无法填充已编译的div
编译了book.contents.name

如何在我的例子中使用$compile和compile指令。 或者任何能够在elem.data('event',{})上将event.rate、event.title、event.inventory映射到book.contents.date、book.contents.name、2/10值的变通方法

这是compile指令

编译 和dragme.html

{{book.contents['date']}
2/10

非常感谢。

无法操作嵌套编译第一次编译用于外部事件框显示。第二个via$compile服务无法将编译后的标题附加到event.title。因此,我通过访问book.contents.name来解决问题 并手动解析html字符串以仅获取标题部分

link: function(scope, elem, attr, ctrl) {

      var bookName = scope.book.contents.name.trim();
      var n = scope.book.contents.name.indexOf("</span><br><span>");
      var titleShrink = bookName.substring(10, n).trim();

      elem.data('event', {
        rate:  scope.book.contents.date,// as the event rate
        title: titleShrink,// as the event title
        stick: true // maintain when user navigates (see docs on the renderEvent method)

      });
链接:函数(作用域、元素、属性、ctrl){
var bookName=scope.book.contents.name.trim();
var n=scope.book.contents.name.indexOf(“
”); var titleShrink=bookName.substring(10,n).trim(); 元素数据(“事件”{ 速率:scope.book.contents.date,//作为事件速率 标题:titleShrink,//作为事件标题 stick:true//在用户导航时进行维护(请参阅renderEvent方法中的文档) });

无法操作嵌套编译第一次编译用于显示外部事件框。第二次通过$compile服务无法将编译后的标题附加到event.title。因此,我通过访问book.contents.name找到了一个解决方法 并手动解析html字符串以仅获取标题部分

link: function(scope, elem, attr, ctrl) {

      var bookName = scope.book.contents.name.trim();
      var n = scope.book.contents.name.indexOf("</span><br><span>");
      var titleShrink = bookName.substring(10, n).trim();

      elem.data('event', {
        rate:  scope.book.contents.date,// as the event rate
        title: titleShrink,// as the event title
        stick: true // maintain when user navigates (see docs on the renderEvent method)

      });
链接:函数(作用域、元素、属性、ctrl){
var bookName=scope.book.contents.name.trim();
var n=scope.book.contents.name.indexOf(“
”); var titleShrink=bookName.substring(10,n).trim(); 元素数据(“事件”{ 速率:scope.book.contents.date,//作为事件速率 标题:titleShrink,//作为事件标题 stick:true//在用户导航时进行维护(请参阅renderEvent方法中的文档) });

var compiled = $compile('<div class="left content" compile="book.contents.name" id="book_{{book.id}}"></div>')(scope);
app.directive('compile', ['$compile', function ($compile) {

  return function (scope, element, attrs) {

        scope.$watch(
            function(scope) {
                // watch the 'compile' expression for changes
                return scope.$eval(attrs.compile);
            },
            function(value) {
                // when the 'compile' expression changes
                // assign it into the current DOM
                element.html(value);

                // compile the new DOM and link it to the current
                // scope.
                // NOTE: we only compile .childNodes so that
                // we don't get into infinite loop compiling ourselves
                $compile(element.contents())(scope);
            }
        );
    };
}]);
<script type="text/ng-template" id="dragme.html">
                       <div class="circle">
                                  {{book.contents['date']}}
                       </div>
                       <!-- THIS IS THE DIV THAT SHOULD BE REPLACED -->
                       <div class="left content"  id="book_{{book.id}}">

                       </div>

                       <div class="left rating">
                            2/10
                       </div>

                       <div class="clear">
                       </div>
                   </script>
link: function(scope, elem, attr, ctrl) {

      var bookName = scope.book.contents.name.trim();
      var n = scope.book.contents.name.indexOf("</span><br><span>");
      var titleShrink = bookName.substring(10, n).trim();

      elem.data('event', {
        rate:  scope.book.contents.date,// as the event rate
        title: titleShrink,// as the event title
        stick: true // maintain when user navigates (see docs on the renderEvent method)

      });