gpt4 book ai didi

angularjs - 在 Angular 中重复嵌入 - 访问当前项目的最佳实践?

转载 作者:行者123 更新时间:2023-12-04 20:39:46 25 4
gpt4 key购买 nike

我编写了以下指令:

transclude: true,
scope: { items: '=' }
...
<div class="row" ng-repeat="item in items">
<div class="col-xs-9">
<ng-transclude></ng-transclude>
</div>
</div>

使用此指令时执行以下操作是否合法/良好做法?
cakes = [ { name: 'blueberry cheesecake', color: 'blue' }, { name: 'rocky road', color: 'mostly brown' } ]
...
<custom-list items="cakes">
<h5>{{$parent.item.name}}</h5>
</custom-list>

我专门说的是 $parent.方面。

最佳答案

Angular 已经认识到更灵活 ng-transclude将是有益的:

https://github.com/angular/angular.js/issues/5489

建议的解决方法之一是为 ng-transclude 定义您自己的覆盖。这允许您执行以下操作:

<div ng-transclude="sibling"></div> <!-- Original behaviour -->
<div ng-transclude="parent"></div> <!-- Takes from where transclusion happens -->
<div ng-transclude="child"></div> <!-- Takes from where transclusion happens, but creates a new child scope -->

自定义来源 ng-transclude :
.config(function($provide){
$provide.decorator('ngTranscludeDirective', ['$delegate', function($delegate) {
// Remove the original directive
$delegate.shift();
return $delegate;
}]);
})

.directive( 'ngTransclude', function() {
return {
restrict: 'EAC',
link: function( $scope, $element, $attrs, controller, $transclude ) {
if (!$transclude) {
throw minErr('ngTransclude')('orphan',
'Illegal use of ngTransclude directive in the template! ' +
'No parent directive that requires a transclusion found. ' +
'Element: {0}',
startingTag($element));
}

var iScopeType = $attrs['ngTransclude'] || 'sibling';

switch ( iScopeType ) {
case 'sibling':
$transclude( function( clone ) {
$element.empty();
$element.append( clone );
});
break;
case 'parent':
$transclude( $scope, function( clone ) {
$element.empty();
$element.append( clone );
});
break;
case 'child':
var iChildScope = $scope.$new();
$transclude( iChildScope, function( clone ) {
$element.empty();
$element.append( clone );
$element.on( '$destroy', function() {
iChildScope.$destroy();
});
});
break;
}
}
}
})

关于angularjs - 在 Angular 中重复嵌入 - 访问当前项目的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569223/

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