2014-08-28 100 views
0

我有一個可以正常工作的自定義markdown指令。現在我想在通過markdown指令加載的內容中使用自定義的youtube指令。 youtube指令本身工作正常,但只要我把它放在markdown文件中,就會被角度忽略。指令中的指令


下工作正常(但不是我想做的事):

的.html

<div markdown link="contentfile.md"> 
</div> 

<div my-youtube code="'videoidhere'"></div> 

.MD

markdown content here 

下面就是我想要做的(但不工作):

的.html

<div markdown link="contentfile.md"> 
</div> 

.MD

markdown content here 

<div my-youtube code="'videoidhere'"></div> 

more markdown content here 

在第二種情況下,它似乎如果YouTube指令從未被調用過。

在評估markdown指令之後,我需要做些什麼來告訴angular來評估該指令?


爲了完整起見,這裏的指令:

降價:

app.directive('markdown', function($http) { 
    var converter = new Showdown.converter(); 
    return { 
     restrict: 'A', 
     scope: { link: '@' }, 
     link: function (scope, element, attrs) 
     { 
      attrs.$observe('link',function(link) 
      { 
       $http.get('modules/test/files/' + link).success(function(response) 
       { 
        var htmlText = converter.makeHtml(response); 
        return element.html(htmlText); 
       }); 
      }); 
     } 
    }; 
}); 

的YouTube:

app.directive('myYoutube', function($sce) { 
    return { 
    restrict: 'EA', 
    scope: { code:'=' }, 
    replace: true, 
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="{{url}}" frameborder="0" allowfullscreen></iframe></div>', 
    link: function (scope) { 
     scope.$watch('code', function (newVal) { 
      if (newVal) { 
       scope.url = $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal); 
      } 
     }); 
    } 
    }; 
}); 
+2

使用'$ compile'源 – charlietfl 2014-08-28 12:59:58

+0

這適用於:http://jsfiddle.net/Fyysf/28/所以你可能需要'$ compile'的降價返回的HTML。 – 2014-08-28 13:04:17

回答

1

你使用添加到DOM到.md文件markdown指令,但由於未編譯,角度不會註冊它。

像這樣的東西應該工作:

$http.get('modules/test/files/' + link).success(function(response) 
{ 
    var htmlText = converter.makeHtml(response); 
    element.html(htmlText); 
    $compile(element.contents())(scope); 
    return element; 
}); 
+0

這似乎解決了它。謝謝! – Ben 2014-08-28 13:10:40