Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Diretiva para controlar o acesso a elementos html
(function(){
angular.module('acesso',[])
.directive('permissaoAcesso', function(){
return {
restrict: 'A',
link: function ($scope, element, attrs) {
if (attrs.permissaoAcesso === 'block') {
element.attr('disabled', 'disabled');
element.append('<span class="block fa fa-lock"></span>');
}
}
};
});
})();
<!DOCTYPE html>
<html ng-app="acesso">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<title>Blog do Gabriel Feitosa</title>
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
<style>
.block {
margin-left: 5px;
color: red;
}
a[disabled] {
pointer-events: none;
}
td {
padding: 5px
}
</style>
</head>
<body>
<h1>AngularJS: Diretiva de acesso</h1>
<table>
<th>
<tr>
<td></td>
<td>Botão</td>
<td>Link</td>
</tr>
</th>
<tr>
<td>Com Restrição</td>
<td>
<button permissao-acesso="block" onclick="alert('Ops, deu errado!')">Botão</button>
</td>
<td>
<a href="" permissao-acesso="block" onclick="alert('Ops, deu errado!')">Link</a>
</td>
</tr>
<tr>
<td>Sem Restrição</td>
<td>
<button permissao-acesso="no-block" onclick="alert('Aeee \\o/, botão liberado!')">Botão</button>
</td>
<td>
<a href="" permissao-acesso="no-block" onclick="alert('Aeee \\o/, link liberado!')">Link</a>
</td>
</tr>
</table>
<footer>
<hr/>
<a href="http://gabrielfeitosa.com"> Blog do Gabriel Feitosa</a>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment