【发布时间】:2013-04-29 11:10:10
【问题描述】:
我有一个指令,根据 ng-repeat 项目数据(来自数据库),使用 switch case 构建自定义 HTML:
app.directive('steps', function($compile){
return {
'restrict': 'A',
'template': '<h3>{{step.name}}</h3><ul ng-repeat="opt in step.opts"><div ng-bind-html-unsafe="extra(opt)"></div></ul>',
'link': function($scope, $element){
$scope.extra = function(opt){
switch ($scope.step.id){
case 1:
return "<div>Some extra information<select>...</select></div>"
case 2:
return "<div><input type='checkbox' ng-model='accept'> Accept terms</div>"
case 3:
return "<div>{{step.title}}<select multiple>...</select></div>"
}
}
}
}
});
上面的代码有效,但函数内部的可绑定{{step.title}} 无效。我试过$compile(html)($scope),但它给了我Error: 10 $digest() iterations reached. Aborting!。我该如何处理?
【问题讨论】:
-
我认为这不是解决问题的正确“角度”方式。与其调用
extra函数,不如有一个更大的模板,用ng-switch之类的来改变要显示的内容不是更清楚吗? -
UI 数据来自数据库,完全依赖于数据库。这是一个“计划组合”选择器的表单向导,它会随着用户的变化而变化
标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat