Last active
March 24, 2016 08:55
-
-
Save zhentian-wan/c253c2a2f626b0cb3529 to your computer and use it in GitHub Desktop.
Angular 1.5 Multi Transcludion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> | |
<script src="script.js"></script> | |
</head> | |
<body ng-app="app" ng-controller="AppCtrl as main"> | |
<ng-details> | |
<detail-title message="main.detail.title"></detail-title> | |
<detail-content-text message="main.detail.text"></detail-content-text> | |
<detail-content-image message="main.detail.image"></detail-content-image> | |
</ng-details> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('app', []); | |
app.controller('AppCtrl', function() { | |
this.detail = { | |
title: "Detail title", | |
text: "Text Content", | |
image: "Image Content" | |
}; | |
}); | |
app.component('detailContentImage', { | |
bindings: { | |
message: '<' // one way binding | |
}, | |
controller: function () { | |
}, | |
controllerAs: 'vm', | |
template: '<h2>{{vm.message}}</h2>' | |
}); | |
app.component('detailContentText', { | |
bindings: { | |
message: '<' | |
}, | |
controller: function () { | |
}, | |
controllerAs: 'vm', | |
template: '<h3>{{vm.message}}</h3>' | |
}); | |
app.component('detailTitle', { | |
bindings: { | |
message: '<' | |
}, | |
controller: function () { | |
}, | |
controllerAs: 'vm', | |
template: '<span>{{vm.message}}</span>' | |
}); | |
app.component('ngDetails', { | |
bindings: {data: '<'}, | |
transclude: { | |
'title': 'detailTitle', | |
'textContent': 'detailContentText', | |
'imageContent': 'detailContentImage' | |
}, | |
controller: function () { | |
this.toggle = true; | |
this.toggleIt = function () { | |
this.toggle = !this.toggle; | |
} | |
}, | |
controllerAs: 'vm', | |
template: [ | |
'<div class="details">', | |
'<div class="summary" ng-click="open = !open">', | |
'{{ open ? \'▾\' : \'▸\' }}', | |
'<span ng-transclude="title">default title</span>', | |
'</div>', | |
'<div class="content" ng-if="vm.toggle" ng-show="open" ng-transclude="textContent">default text</div>', | |
'<div class="content" ng-if="!vm.toggle" ng-show="open" ng-transclude="imageContent">default image</div>', | |
'</div>', | |
'<button ng-click="vm.toggleIt()">Toggle it: {{vm.toggle}}</button>' | |
].join('') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment