AngularJS - Directives and Scope
by Shank09
HTML
<script src="http://code.angularjs.org/1.1.4/angular.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/tippy.css">
<script src="https://unpkg.com/[email protected]/dist/tippy.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/js/materialize.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<body ng-app="myapp">
<div class="row">
<!-- CONTROLLER 2-->
<div class="col s12 m6" ng-controller="controller1">
<div class="card fill1">
<div class="card-content">
<span class="card-title">{{title}}</span>
<i class="material-icons tippy c-pointer" data-theme="light" data-trigger="click" data-interactive="true" data-animatefill="false" data-arrow="true">settings</i>
<div id="settings-controller2" parent="tippy" settings></div>
<div class="filler"></div>
Cache is : <b>{{cache}}</b>
</div>
</div>
</div>
<!-- CONTROLLER 2-->
<div class="col s12 m6" ng-controller="controller2">
<div class="card">
<div class="card-content">
<span class="card-title">{{title}}</span>
<i class="material-icons tippy2 c-pointer" data-theme="light" data-trigger="click" data-interactive="true" data-animatefill="false" data-arrow="true">settings</i>
<div id="settings-controller" parent="tippy2" settings></div>
<div class="filler"></div>
Cache is : <b>{{cache}}</b>
</div>
</div>
</div>
</div>
<script...
CSS
.filler {
height: 200px;
}
.fill1 {
background-color: aqua;
}
body {
background-color: #f3f3f3;
}
.tippy-tooltip[data-template-id="#setting-template"] {
/* Your styling here. Example: */
padding: 0px;
width: 250px;
}
.c-pointer {
cursor: pointer;
}
.col-title {
padding-top: 5px;
}
.col-item {
margin-top: 2px;
}
JavaScript
var myapp = angular.module('myapp', [])
.directive('settings', function() {
return {
templateUrl: 'tippy-template.html',
controller: function($scope, $element, $attrs){
$scope.parent = $element;
$scope.tippyClass = $attrs.parent;
}
};
})
.directive('tippy', function() {
return {
controller: function ($attrs, $scope, $element) {
console.log($attrs.id);
tippy('.'+$scope.tippyClass, {
position: 'bottom',
animation: 'shift',
arrow: true,
interactive: true,
arrowSize: 'big',
distance: 20,
html: $element[0],
appendTo: $scope.parent[0]
})
}
};
})
.controller('controller2', function($scope) {
$scope.title = "Controller 2";
$scope.cache = true;
})
.controller('controller1', function($scope) {
$scope.title = "Controller 1";
$scope.cache = false;
});