直接上代码
app.controller('MainCtrl', function($scope) {
$scope.person = {
name: 'John Doe',
profession: 'Fake name'
};
$scope.header = 'Person';
});
app.directive('person', function() {
return {
restrict: 'EA',
scope: {
header: '='
},
transclude:true,
template: '<div ng-transclude></div>',
link: function(scope, element, attrs) {
scope.person = {
name: 'Directive Joe',
profession: 'Scope guy'
};
scope.header = 'Directive\'s header';
}
};
});
<body ng-controller="MainCtrl">
<person header="header">
<h2>{{header}}</h2>
<p>Hello, I am {{person.name}} and,</p>
<p>I am a {{person.profession}}</p>
</person>
</body>
以上代码会变成
<body ng-controller="MainCtrl" class="ng-scope">
<person header="header" class="ng-isolate-scope"><div ng-transclude="">
<h2 class="ng-scope ng-binding">Directive's header</h2>
<p class="ng-scope ng-binding">Hello, I am John Doe and,</p>
<p class="ng-scope ng-binding">I am a Fake name</p>
</div></person>
</body>
即原有的属性不会被替换,而原来没有的属性,则会使用新的
如果想通过指令来控制接受父级的scope值还是子集的scope值,可以通过如下代码区分
app.directive('person', function() {
return {
restrict: 'EA',
scope: {
header: '='
},
transclude:true,
link: function(scope, element, attrs, ctrl, transclude) {
scope.person = {
name: 'Directive Joe',
profession: 'Scope guy'
};
scope.header = 'Directive\'s header';
transclude(scope.$parent, function(clone, scope) {
element.append(clone);
});
}
};
});
和
app.directive('person', function() {
return {
restrict: 'EA',
scope: {
header: '='
},
transclude:true,
link: function(scope, element, attrs, ctrl, transclude) {
scope.person = {
name: 'Directive Joe',
profession: 'Scope guy'
};
scope.header = 'Directive\'s header';
transclude(scope, function(clone, scope) {
element.append(clone);
});
}
};
});
以上两组代码中,transclude传递的参数中,第一个参数分别传递的为scope.$parent和scope
参考自
http://angular-tips.com/blog/2014/03/transclusion-and-scopes/
分享到:
相关推荐
有完整的例子和学习地址供大家参考,以帮助大家正确理解transclude的用法。有不懂的问题可以留言,相互交流
视图是一组可见的屏幕元素,Angular 可以根据你的程序逻辑和数据来选择和修改它 们。 每个应用都至少有一个根组件。 组件使用服务。服务会提供那些与视图不直接相关的功能。服务提供商可以作为依赖被注入到组件中, ...
主要给大家介绍了关于Angular项目中$scope.$apply()方法使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Angularjs具有一定的参考学习价值,需要的朋友们下面跟着小编一起来看看吧。
用法将 angular-import-scope 添加到您的项目中: bower install --save angular-import-scope将其添加到您的 HTML 文件中: < script src =" bower_components/angular-import-scope/dist/angular-import-scope....
Angular父子组件以及组件之间通讯 @Input @Ouput @ViewChild
Angular父子组件以及组件之间通讯示例代码.zip
本篇文章主要介绍了angular中实现控制器之间传递参数的方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
angular-flot, angular.js 和Flot图表库之间的绑定 角 flot 包装Flotcharts的一个 Angular 指令。安装这里库是作为 Bower 组件和NPM模块提供的:Bower: bower install angular-flotnpm install
angular-snapscroll, 在 Angular 中,垂直滚动和捕捉功能 角 snapscroll angular-snapscroll向 Angular 添加垂直scroll-and-snap功能。js仅实现只需要 Angular 内核6.2kB 缩小时,压缩时 2.3 kB
揭秘angular2 中文pdf电子版是一本专业的angular技术书籍,图文清晰,结构明朗,该书覆盖angular基础、架构以及技术应用等各方面知识,内容非常详细,有兴趣的朋友欢迎前来下载!
Angular 2 中文文档,还不错,可以作为参考。Angular 2 中文文档,还不错,可以作为参考。
主要给大家深入的介绍了关于Angular.JS中Scope继承的相关资料,...JS中scope之间的继承关系使用JavaScript的原型继承方式实现,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
在angular项目中集成海康 视频web插件 V1.4.1,利用angular中的directive进行订制。
世界上最好用的基于 Angular 和 Angular Material 的树形表格组件
主要研究一下如何使用ng-transclude指令,以及指令的transclude选项的相关资料,文中介绍的非常详细,并给出了完整的示例代码大家参考学习,需要的朋友们下面来一起看看吧。
Key Features, Get up to date with the latest changes to Angular 2, including the improvements to directives, change detection, dependency injection, router, and moreUnderstand Angular 2's new ...
Angular-angular2-mdl.zip,基于Material Design Lite的Angular 2、4、5、6、7、8组件、指令和样式(NPM:@Angular MDL/Core)基于Material Design Lite的Angular 8组件、指令和样式https://getmdl.io(v:1.3.0)。...
如何安装 bower install angular-multi-transclude主张在编写可重用指令时,您希望能够通过将内容转换到多个区域中的指令模板来使用自定义元素和业务逻辑扩展指令。 您希望在不修改指令的代码和模板的情况下执行此...