gpt4 book ai didi

javascript - 如何通过嵌入将父指令属性值传递到子指令范围?

转载 作者:行者123 更新时间:2023-12-03 07:03:36 25 4
gpt4 key购买 nike

我试图通过 ng-transclude 将属性值从指令传递到子指令的范围。我尝试过使用 =、@ 和 & 进行范围绑定(bind),但我仍然感到困惑。我希望子指令继承父指令的属性。任何帮助将不胜感激!

我在这里做了一个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 }}">'
};
});

关于javascript - 如何通过嵌入将父指令属性值传递到子指令范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36923570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com