AngularJS - 从子指令访问父指令性质 [英] AngularJS - accessing parent directive properties from child directives
问题描述
这应该不是太难做的事情,但我无法弄清楚如何最好地做到这一点。
This should not be too hard a thing to do but I cannot figure out how best to do it.
我有一个父指示,例如:
I have a parent directive, like so:
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'
};
});
我怎样才能方便地访问模式
从子指令父指令和编辑
属性?在我的链接的功能,我可以访问父范围? - 我应该使用 $观看
来观看这些特性
How can I easily access the model
and editing
properties of the parent directive from the child directive? In my link function I have access to the parent scope - should I use $watch
to watch these properties?
放在一起,我想拥有的是:
Put together, what I'd like to have is:
<editable-fieldset model="myModel">
<editable-string label="Some Property" field="property"></editable-string>
<editable-string label="Some Property" field="property"></editable-string>
</editable-fieldset>
这个想法是有一组默认显示的字段。如果点击,就成为输入,并且可以进行编辑。
The idea is to have a set of fields displayed by default. If clicked on, they become inputs and can be edited.
推荐答案
这个灵感后SO ,我已经在这个plunker 得到了工作液这里。
Taking inspiration from this SO post, I've got a working solution here in this plunker.
我不得不改变了不少。我选择了有一个孤立的范围 editableString
以及因为它更容易以正确的价值观为模板进行绑定。否则,你将不得不使用编译
或其它方法(如 $ transclude
服务)。
I had to change quite a bit. I opted to have an isolated scope on the editableString
as well because it was easier to bind in the correct values to the template. Otherwise, you are going to have to use compile
or another method (like $transclude
service).
下面是结果:
JS:
var myApp = angular.module('myApp', []);
myApp.controller('Ctrl', function($scope) {
$scope.myModel = { property1: 'hello1', property2: 'hello2' }
});
myApp.directive('editableFieldset', function () {
return {
restrict: 'E',
scope: {
model: '='
},
transclude: true,
replace: true,
template: '<div class="editable-fieldset" ng-click="edit()"><div ng-transclude></div></div>',
link: function(scope, element) {
scope.edit = function() {
scope.editing = true;
}
},
controller: ['$scope', function($scope) {
this.getModel = function() {
return $scope.model;
}
}]
};
});
myApp.directive('editableString', function () {
return {
restrict: 'E',
replace: true,
scope: {
label: '@',
field: '@'
},
template: '<div><label>{{ label }}</label><p>{{ model[field] }}</p></div>',
require: '^editableFieldset',
link: function(scope, element, attrs, ctrl) {
scope.model = ctrl.getModel();
}
};
});
HTML:
<body ng-controller="Ctrl">
<h1>Hello Plunker!</h1>
<editable-fieldset model="myModel">
<editable-string label="Some Property1:" field="property1"></editable-string>
<editable-string label="Some Property2:" field="property2"></editable-string>
</editable-fieldset>
</body>
这篇关于AngularJS - 从子指令访问父指令性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!