app.directive('remoteForm', function($http){ function link(scope, formElement, attrs, formController) { scope.input = {}; scope.submit = function (){ $http.post(attrs.remoteForm, scope.input) .success(function(data, status, headers, config) { // $window.location.reload(); }). error(function(data, status, headers, config) { angular.forEach(data, function (value, key){ if (key == 'non_field_errors') return; if (formController[key]) formController[key].$setValidity('remote', false); }) }); }; formElement.submit(scope.submit); } return { require: 'form', scope: { }, link: link, compile: function (temaplateElement, templateAttrs) { return { pre: function (scope, element, attrs){ // element.find('[remote-form-control]').each(function (){ // var name = $(this).attr('remote-form-control'); // $(this).attr('ng-model', 'input.' + name); // }); }, post: link } } }; }); app.directive('remoteFormControl', function(){ return { require: '^remoteForm', compile: function (temaplateElement, templateAttrs) { return { pre: function (scope, element, attrs){ if (element.attr('remote-form-control')){ var name = element.attr('remote-form-control'); $(element).attr('ng-model', 'input.' + name); input.after('{{ errors.' + name + ' }}'); $(element).closest('.form-group').attr('ng-class', '{"has-error": form.'+name+'.$invalid}'); } }, post: function(scope, element, attrs) { } } } } });