0

Я пытаюсь передать значение атрибута из директивы в область действия дочерней директивы через ng-transclude. Я пробовал использовать =, @, & для привязки к сфере видимости, однако я все еще ошеломлен. Я хочу, чтобы директива child наследовала атрибут из родительской директивы. Любая помощь будет оценена!Как передать значение атрибута родительской директивы дочерней области действия через 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> 

ответ

1

Демо: 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 }}">' 
    }; 
}); 
+0

Это не то, что я пытаюсь сделать. Я пытаюсь не добавлять ярлык к дочернему элементу. Я бы хотел, чтобы он наследовал атрибут от родителя. Я вложил в jsfiddle то, что ожидаю. – jemiloii

+0

Хорошо. Plz см. Обновленный ответ. – softvar

+0

@softvar Я также пытался получить значение te родителя с ключом require, но это не сработало. Родитель не был найден, и поэтому у него не было дополнительного параметра в функции ссылки. Зачем? Это потому, что родитель переводит? – Michelangelo

Смежные вопросы