0
我有一個問題涉及到AngularJS中的條件部分類似視圖。下面的例子不是非常優化的。它還返回屬於行$compile(element.contents())(scope)
的錯誤,該錯誤表示我不能使用scope
作爲函數 - 但否則它將無法正確渲染所有內容。使用情況如下:AngularJS中的條件視圖
我請求路由在
$http
返回對象我
ng-repeat
是返回的對象數組對於每一個對象的數組(讓我們稱之爲
obj
)給出了一個obj.view_edit
值。如果
obj.type
等於plugin
,一個<plugin></plugin>
指令插入
這將是這樣的:
<plugin view="content.view_edit"></plugin>
我的指令看起來像:
directive('plugin', function($compile) {
var linker = function(scope, element, attrs) {
console.log(scope.view);
element.html(scope.view).show();
$compile(element.contents())(scope);
}
return {
restrict:"E",
link: linker,
scope: {
view:'='
}
}
})
你有更好的解決方案?
謝謝!我用[ngSwitch](https://docs.angularjs.org/api/ng/directive/ngSwitch)'ng-switch =「content.type」'和'ng-switch-when =每個類型的「插件」。 – barfoos 2014-11-22 08:25:27