I have a directive of stripe, Which passing a value a from directive to controller.
Directive
angular.module('stripe', []).directive('stripeForm', ['$window',
function($window) {
var directive = { restrict: 'A' };
directive.link = function(scope, element, attributes) {
var form = angular.element(element);
form.bind('submit', function() {
var button = form.find('button');
button.prop('disabled', true);
$window.Stripe.createToken(form[0], function() {
button.prop('disabled', false);
var args = arguments;
scope.$apply(function() {
scope.$eval(attributes.stripeForm).apply(scope, args);
});
});
});
};
return directive;
}]);
Controller :
angular.module('myApp', ['stripe'])
.controller('IndexController', function($scope, $http) {
$scope.saveCustomer = function(status, response) {
$http.post('/save_customer', { token: response.id });
};
});
HTML
<form stripe:form="saveCustomer">
<fieldset>
<input type="text" size="20" data-stripe="number"/>
<input type="text" size="4" data-stripe="cvc"/>
<input type="text" size="2" data-stripe="exp-month"/>
<input type="text" size="4" data-stripe="exp-year"/>
</fieldset>
<button type="submit">Save</button>
</form>
One of my college says using $eval is not a best practice, So I need a alternative to replace scope.$eval.
I also wondering that how the directive is passing value to controller. Pleae explain the code, How it works.
scope.$apply(function() {
scope.$eval(attributes.stripeForm).apply(scope, args);
});