我需要访问指令创建的模型,同时我需要获取指令中的attrs。如何访问隔离范围的指令attrs?
JS:
module.directive('createControl', function($compile, $timeout){
return {
scope: {
name: '=Name' // Dynamically created ng-model in the directive element
},
link: function(scope, element, attrs){
attrs.$observe('createControl', function(){
attrs.createControl //is empty if scope is an object, otherwise it is passed from html attribute
}
}
HTML:
<div class="control-group" ng-repeat="x in selectedControls">
<div create-control="{{ x }}"></div>
</div>
如果scope
被定义为一个对象,attrs
是空的,否则它是从HTML传递的值。
这种行为的原因是什么?如何获得通过attrs和模型?
“= Name”语法要求您的HTML具有“Name”属性。例如,'
' – 2013-05-07 14:35:44'=在指令html中动态添加的名称属性。所以是的,这是存在的。此外,我可以得到它。 “attrs”的问题。 – I159 2013-05-07 14:50:22
@ I159你可以在你说的名字是动态添加的地方包含你的指令HTML吗? – Langdon 2013-05-07 15:03:15