在angularjs的子指令中访问父范围
我正在尝试访问child指令中的parents指令范围。 我希望child指令继承parents范围在angularjs的子指令中访问父范围,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正在尝试访问child指令中的parents指令范围。 我希望child指令继承parents范围 function parent() { return { scope: { attribute: '=attribute' }, transclude: true, restrict: 'E' } } 上面的代码表示父指
function parent() {
return {
scope: {
attribute: '=attribute'
},
transclude: true,
restrict: 'E'
}
}
上面的代码表示父指令
function child() {
return {
require: '^parent',
scope: {
attribute: '=attribute',
attribute2: '='
}
restrict: 'E'
}
}
上面的代码表示子指令。
我希望子指令从父指令继承属性,如果它是在html中提供的,但我希望能够将其作为非嵌套指令使用
<parent attribute='attr'>
<child attribute2='attr2'></child>
<parent>
<child attribute='attr' attribute2='attr2'></child>
上面的代码是我想要实现的。如果您希望该指令从父级继承,请使用
scope:true
。如果您不希望指令上有作用域,请使用scope:false
。有关详细信息,请参阅。不推荐使用双向(“=”
)绑定。使用它将使迁移到Angular 2+变得困难,因为Angular 2+没有双向绑定。如果您提供人们可以用来重现问题的代码,您将得到更好的答案。看见