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