這不應該太難,但我不知道如何做到最好。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>
的想法是有一組默認顯示的字段。如果點擊它們,它們就成爲輸入並且可以被編輯。
你能舉一個最終標記的樣子嗎? –
我已經在現在 –