There are a couple points to be made here, I think. I'll do my best to answer your question.
Firstly, I just want to point out that restricting access on the front-end alone is not usually a good security practice (any code on the front-end can be manipulated by the user. If the user still has POST privileges on the server, even if it is restricted on the client, there are ways to get around that.) Therefore, if this is a serious question about restricting user access, I would definitely recommend doing that on the server, and not on the client.
Secondly, I'll answer the main question. There are several ways to go about intercepting a request. The usual method is to use an interceptor (blog post by Ben Nadel). Since your question requires you to use a directive for some reason, I might suggest sending an event through $rootScope on form submit, and the HTTP interceptor will look at that event and complete or stop the request. However, I think the better approach would just be to use an interceptor (and no directive) if all you're looking to do is restrict all POST requests.
So, your directive:
// inject $rootScope as a dependency first
link: function(scope, $el, attrs) {
$el.on('submit', stop);
function stop() {
e.preventDefault();
$rootScope.$broadcast('request.restricted', $el);
return false;
}
}
Then, your interceptor:
config.$inject = ['$httpProvider'];
app.config(config);
function config($httpProvider) {
$httpProvider.interceptors.push(interceptor);
interceptor.$inject = ['$q', '$rootScope'];
function interceptor($q, $rootScope) {
var cancel = $q.defer();
return {
request: request
};
function request(config) {
$rootScope.$on('request.restricted', stop);
function stop() {
config.timeout = cancel.promise;
}
return config;
}
}
}
See this question that says how to stop a request from completing.
However
What I would really recommend is just using an interceptor to check if the method used is POST, and if so, stop the request. No directive needed.
config.$inject = ['$httpProvider'];
app.config(config);
function config($httpProvider) {
$httpProvider.interceptors.push(interceptor);
interceptor.$inject = ['$q'];
function interceptor($q) {
var cancel = $q.defer();
return {
request: request
};
function request(config) {
if(config.method.toLowerCase() === 'post') {
config.timeout = cancel.promise;
}
return config;
}
}
}
Better yet, do it on the server