我我不能申請$compile(element.contents())(scope);
設法做到這一點做到以下幾點:
添加指令在html:
<my-directive update-data-trigger="someObject.content" data="someObject"></<my-directive>
的指令:
app.directive("myDirective", function ($compile) { return { replace:true, restrict: "E", scope: { //Data holds the html in the content attribute data: '=', updateDataTrigger: '=' }, link: function ($scope, element) { //Add a watcher to refresh data because the loaded data passed is async $scope.$watch('updateDataTrigger', function(){ //Check if data passed has been loaded with our desired object if($scope.updateDataTrigger != null) { //Do some content manipulation here //Append directives to the content as well //render as html element.html(data.content); $compile(element.contents())($scope); } }); } } });
只是要指令,將字符串作爲參數代替一些文本,編譯結果並將其添加到DOM。 –
感謝您的提示,這是我第一次做這樣的實現 –