我在一个我为下拉替换写的指令中有一个非常奇怪的行为。使用的模型可以使用子节点无限嵌套。 (HAML-Code):AngularJS:指令的模板不能正确渲染
.control-group
-# use the angular directive "dropdown-tree" that can handle unlimited nested models!
.controls{ "dropdown-tree" => "", "ng-model" => "categories_as_tree", "current-value" => "currentCategory", "dropdown-placeholder" => "choose category", "ng-disabled" => "!categories || categories.length==0", "on-change"=>"fetchProducts(category)" }
%h4 Products
.control-group
.controls{ "dropdown-tree" => "", "ng-model" => "products_as_tree", "current-value" => "currentProduct", "dropdown-placeholder" => "choose product", "ng-disabled" => "!products || products.length==0" }
正如我到目前为止所遇到的,该模型的行为是绝对正确的!在调试视图中或者通过console.log或者$ log whatever,但是指令的渲染会变得混乱起来,显示项目翻倍或者甚至倍增,取决于您切换下拉的次数。
咖啡剧本是这样的,这是很容易的代码:该指令的
# use array -> being resistend against uglifiers mangle
mymodule.directive 'dropdownTree', [ ->
return {
templateUrl: '/angular/templates/dropdown_tree'
,
#require: 'ngModel', # don't need that so far, we keep things simple!
scope: {
ngModel: '=',
dropdownPlaceholder: '@',
currentValue: '=',
ngDisabled: '=',
onChange: '&'
},
link: (scope, element, attr, ngModelCtrl) ->
# fake the ng-change
scope.$watch('currentValue', ->
scope.onChange()
, true)
scope.selectValue = (value) ->
scope.currentValue = value
}
]
视图模板代码得到由它写在HAML,看起来像这样的轨道控制器 交付。它采用无限嵌套如果存在子节点:
.btn-group{ :name => "FIXME", "ng-required" => "true" }
%a.btn.btn-default.dropdown-toggle{ "data-toggle" => "dropdown", href: "#", "ng-disabled"=>"ngDisabled" }
{{currentValue.name || dropdownPlaceholder }}
%span.caret
%ul.dropdown-menu{ "ng-show" => "!ngDisabled" }
%div{ "ng-repeat" => "model in ngModel", "ng-include" => "'node.html'" }
%script{ :type => "text/ng-template", :id => "node.html" }
%li{ "ng-click" => "selectValue(model)" }
%a
{{model.name}}
%ul{ "ng-repeat" => "model in model.children", :style => "margin-left: 10px;" }
%div{ "ng-include" => "'node.html'" }
我没有与这只是一个问题的任何第二个下拉不正确地更新其观点,型号不对。我想知道你能不能帮助或看到一些不合法的东西。
亲切的问候, 亚历
只是好奇,为什么你使用on-change而不是ng-change? – jpmorin
我对HAML并不熟悉,但是按照你的缩进,这是我的理解:你首先在'ul'的内部循环'div',在'div'里面放置'li'。在你循环的'ul'上有'li'的兄弟,你在'ul'中使用'div'来启动下一个子级别......当我看到它时,你的层次结构如下所示: 'ul > DIV [循环]> [li>系统| UL [循环]> DIV> [li>系统| UL [循环]> DIV ...'。 AFAIK,这不是有效的HTML。 'ul'中唯一有效的元素是'li'。 – jpmorin
我确实使用on-change,因为指令读取的值是在自定义作用域中调用的回调函数$ watch ...我不想混淆该指令的其他用户的ng-change,因为ng-change是原生指令... 是的,我知道这是不好的HTML。原因是增加了一些能够接收正确项目的块。我将在下面检查你的帖子。非常感谢。 – sp33c