aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Minar2010-10-22 16:44:14 -0700
committerMisko Hevery2010-10-23 14:22:54 -0700
commit04a4d8b061d98f46dc97fb550d388d248845b369 (patch)
tree65ffa23fb4edf41438c091655be1f4f9536975f5
parentbbd87c9425ae984f632783b1f33f415ec1747ca6 (diff)
downloadangular.js-04a4d8b061d98f46dc97fb550d388d248845b369.tar.bz2
adding ng:submit directive for use with forms
- allows for binding angular expressions to onsubmit events - prevent default submit action (page reload)
-rw-r--r--src/directives.js19
-rw-r--r--test/directivesSpec.js14
2 files changed, 33 insertions, 0 deletions
diff --git a/src/directives.js b/src/directives.js
index 148e1f86..49f0343d 100644
--- a/src/directives.js
+++ b/src/directives.js
@@ -218,6 +218,25 @@ angularDirective("ng:click", function(expression, element){
};
});
+
+/**
+ * Enables binding angular expressions to onsubmit events.
+ *
+ * Additionally it prevents the default action (which for form means sending the request to the
+ * server and reloading the current page).
+ */
+angularDirective("ng:submit", function(expression, element) {
+ return function(element) {
+ var self = this;
+ element.bind('submit', function(event) {
+ self.$tryEval(expression, element);
+ self.$root.$eval();
+ event.preventDefault();
+ });
+ };
+});
+
+
angularDirective("ng:watch", function(expression, element){
return function(element){
var self = this;
diff --git a/test/directivesSpec.js b/test/directivesSpec.js
index 341e9031..0e99a63f 100644
--- a/test/directivesSpec.js
+++ b/test/directivesSpec.js
@@ -195,6 +195,20 @@ describe("directives", function(){
});
});
+
+ describe('ng:submit', function() {
+ it('should get called on form submit', function() {
+ var scope = compile('<form action="" ng:submit="submitted = true">' +
+ '<input id="submit" type="submit"/>' +
+ '</form>');
+ scope.$eval();
+ expect(scope.submitted).not.toBeDefined();
+
+ browserTrigger(element.children()[0]);
+ expect(scope.submitted).toEqual(true);
+ });
+ });
+
it('should ng:class', function(){
var scope = compile('<div class="existing" ng:class="[\'A\', \'B\']"></div>');
scope.$eval();