2013-08-05 42 views

回答

8

您可以注入$ compile服務,並在需要時進行$編譯。 $compile('<p>{{total}}</p>')(scope)docs的示例。

在實踐中,你可能會想要做這樣的事情:

//Example as a directive's link function 
function link(scope, element, attributes){ 
    scope.name = "world"; 
    template = "<p>hello {{name}}</p>"; //this could come from anywhere 
    element.html(template); 
    $compile(element.contents())(scope); 
} 

這個例子追加編譯內容而不是替換它:

function link(scope, element, attributes){ 
    scope.something = "this is bananas"; 
    $compile("<p>{{something}}</p>")(scope, function(cloned, scope){ 
    element.append(cloned); 
    }); 
} 
+0

我這樣做時沒有出錯,但模板也沒有被編譯。然而,我在模板中調用的控制器正在運行。 –

+0

對不起,我從內存中輸入並忘記了一點,'contents()',因爲你不想傳遞該元素,而只是其內容。我現在編輯它應該工作。下面是一個在pnkrr中的工作示例:http://plnkr.co/edit/M5np1QTZgcGuQOfeIHca?p=preview(點擊左側的app.js查看代碼) –

+0

謝謝,這已經越來越接近了。唯一的是我不想使用指令的元素,我基本上是創建一個新的,並將其傳遞給模態調用。有沒有辦法獲得編譯結果,以便我可以將它傳遞給我的模態插件調用? –