这不应该太难,但我不知道如何做到最好。AngularJS - 从子指令访问父指令属性
我有一个父指示,例如:
directive('editableFieldset', function() {
return {
restrict: 'E',
scope: {
model: '='
},
replace: true,
transclude: true,
template: '
<div class="editable-fieldset" ng-click="edit()">
<div ng-transclude></div>
...
</div>',
controller: ['$scope', function ($scope) {
$scope.edit = ->
$scope.editing = true
// ...
]
};
});
和一个小孩指令:
.directive('editableString', function() {
return {
restrict: 'E',
replace: true,
template: function (element, attrs) {
'<div>
<label>' + attrs.label + '</label>
<p>{{ model.' + attrs.field + ' }}</p>
...
</div>'
},
require: '^editableFieldset'
};
});
我怎么能轻易地从孩子的指令访问父指令的model
和editing
性质?在我的链接功能中,我可以访问父范围 - 我应该使用$watch
来观看这些属性吗?
放在一起,我想拥有的是:
<editable-fieldset model="myModel">
<editable-string label="Some Property" field="property"></editable-string>
<editable-string label="Some Property" field="property"></editable-string>
</editable-fieldset>
的想法是有一组默认显示的字段。如果点击它们,它们就成为输入并且可以被编辑。
你能举一个最终标记的样子吗? –
我已经在现在 –