Skip to content

Commit 8aee32c

Browse files
committed
adds section and forms and demonstration
1 parent 0df4a17 commit 8aee32c

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

09-0-forms.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>ngResource</title>
5+
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
6+
7+
<script>
8+
//controls our form
9+
var app = angular.module('formApp', []);
10+
app.controller('FormController', function(){
11+
this.userinput = {}
12+
this.submit = function(){
13+
alert('Hello ' + this.userinput.name + '!');
14+
this.userinput = {}
15+
}
16+
});
17+
</script>
18+
<style>
19+
/*This styles our input forms*/
20+
input.ng-dirty.ng-invalid{
21+
border:red 1px solid;
22+
}
23+
input.ng-dirty.ng-valid{
24+
border:green 1px solid;
25+
}
26+
</style>
27+
</head>
28+
<body ng-app="formApp">
29+
<div style="margin:0 40%;">
30+
<!-- Set the form to 'novalidate' to prevent default browser validation -->
31+
<!-- user the userForm.$valid &&... in ng-submit to prevent submission until everything is complete. -->
32+
33+
<form name="userForm" ng-controller="FormController as formCtrl"ng-submit="userForm.$valid && formCtrl.submit()" novalidate>
34+
35+
<!-- Set both the input's as required to ensure we add them and can validate them -->
36+
Name: <input type="text" ng-model="formCtrl.userinput.name" required /><br />
37+
Email: <input type="email" ng-model="formCtrl.userinput.email" required /><br />
38+
39+
<!-- Inline if-statement to display valid/invalid -->
40+
Your form is: {{userForm.$valid? 'valid' : 'invalid '}}
41+
<input type="submit" value="Submit!" />
42+
43+
</form>
44+
</div>
45+
</body>
46+
</html>

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ template:"<div><div><span ng-transclude></span></div></div>",
172172
//...
173173
```
174174
- `link:function()` executes when the directive sets up. Can be useful in setting `timeout`s, or binding `event`s to the element
175-
- `controller:function()` allows you to set up scope properties and create an interface for other directives to interact with.
175+
- `controller:function()` allows you to set up scope properties and create an interface for other directives to interact with. This is also useful if you have functions in your directive that need to be executed by only your directive.
176+
- `controllerAs: 'String'` allows you to use the [Controller As]() syntax in your directives. *note that now we don't bind things to `scope` we now bind it to `this`.* Refer notes above for more details on the 'controllerAs' syntax.
176177
-`scope:{}` allows you to create a new scope for the element rather than inherit from the parent
177178
- `require:['^directives']` makes it mandatory for the current directive to be nested in the parent to ensure it functions correctly.
178179
A quick gotcha to note, when you name your directive `datePicker` when declaring it, you can refer to it as `date-picker` in your HTML.
@@ -541,7 +542,14 @@ scope.$broadcast('myEventName', 'Bye', 'World');
541542
```
542543
What is the difference between `$emit` and `$broadcast`? As mentioned previously `$emit` propogates the event upwards and all controllers listening for `myEventName` in the parent scopes will be alerted. `$broadcast` does the opposite and propogates the event downwards. Note that both these events will also execute in their own scopes.
543544

544-
#### Providers and Injectors (advanced)
545+
### Forms
546+
*Content from this section (Forms) is based of the course [Shaping Up With AngularJS](http://campus.codeschool.com/courses/shaping-up-with-angular-js/)*
547+
Remember when we discussed [Angluar Classes above](https://github.com/zafarali/learning-angular#angular-classes)? AngluarJS provides us with a couple of other nice tricks and features that make form validations easier. Let's look at each of them step by step and then do an example:
548+
- `ng-submit` is an attribute of the `<form>` element that has the function will be invoked when the user clicks the 'submit' button. Alternatively this function can also be in `ng-click` of the `<button>` element.
549+
- `formname.$valid` is an object available on the global scope where formname is the `name` attribute of the `<form>` element. From this we can see that AngluarJS does alot behind the scene for us and will automatically validate the form. Returns `true` or `false`.
550+
- Coupling the `$valid` with the Angluar Classes we can make a very interactive form! See [here for example](https://github.com/zafarali/learning-angular/blob/master/09-0-forms.html)
551+
552+
### Providers and Injectors (advanced)
545553
- [ ] To be completed:
546554
#### Providers
547555
#### Injectors

0 commit comments

Comments
 (0)