如何通过嵌入将父指令属性值传递给子指令范围?
How to pass a parent directive attribute value to a child directive scope through transclude?
我正在尝试通过 ng-transclude 将属性值从指令传递到子指令的范围。我已经尝试使用 =、@ 和 & 进行范围绑定,但我仍然感到困惑。我希望子指令从父指令继承属性。如有任何帮助,我们将不胜感激!
我在这里做了一个 jsfiddle --> https://jsfiddle.net/egdfLzLj/5/
Javascript
var app = angular.module('app', []);
app.directive('parent', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
label: '@'
},
template: '<section>' +
'<label>{{::label}}' +
'<ng-transclude></ng-transclude>' +
'</label>' +
'</section>'
};
});
app.directive('child', function () {
return {
restrict: 'E',
replace: true,
scope: {
type: '@',
label: '&'
},
template: '<input ng-type="type" ng-value="::label">'
};
});
Html
<parent label="Parent Label">
<child type="text"></child>
</parent>
演示:https://jsfiddle.net/egdfLzLj/2/
HTML
<parent label="Parent Label">
<child type="text"></child>
</parent>
指令
var app = angular.module('app', []);
app.directive('parent', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
label: '@'
},
template: '<section>' +
'<label>{{::label}}' +
'<ng-transclude></ng-transclude>' +
'</label>' +
'</section>'
};
})
app.directive('child', function () {
return {
restrict: 'E',
replace: true,
link: function (scope) {scope.label = scope.$parent.label;},
template: '<input type="text" value="{{ label }}">'
};
});
我正在尝试通过 ng-transclude 将属性值从指令传递到子指令的范围。我已经尝试使用 =、@ 和 & 进行范围绑定,但我仍然感到困惑。我希望子指令从父指令继承属性。如有任何帮助,我们将不胜感激!
我在这里做了一个 jsfiddle --> https://jsfiddle.net/egdfLzLj/5/
Javascript
var app = angular.module('app', []);
app.directive('parent', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
label: '@'
},
template: '<section>' +
'<label>{{::label}}' +
'<ng-transclude></ng-transclude>' +
'</label>' +
'</section>'
};
});
app.directive('child', function () {
return {
restrict: 'E',
replace: true,
scope: {
type: '@',
label: '&'
},
template: '<input ng-type="type" ng-value="::label">'
};
});
Html
<parent label="Parent Label">
<child type="text"></child>
</parent>
演示:https://jsfiddle.net/egdfLzLj/2/
HTML
<parent label="Parent Label">
<child type="text"></child>
</parent>
指令
var app = angular.module('app', []);
app.directive('parent', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
label: '@'
},
template: '<section>' +
'<label>{{::label}}' +
'<ng-transclude></ng-transclude>' +
'</label>' +
'</section>'
};
})
app.directive('child', function () {
return {
restrict: 'E',
replace: true,
link: function (scope) {scope.label = scope.$parent.label;},
template: '<input type="text" value="{{ label }}">'
};
});