AngularJS Directives
ng-
.ng-app
directive initializes an AngularJS application.ng-init
directive initializes application data.ng-model
directive binds the value of HTML controls (input, select, textarea) to application data.Example
<div ng-app="" ng-init="firstName='John'">
<p>Name: <input type="text" ng-model="firstName"></p>
<p>You wrote: {{ firstName }}</p>
</div>
ng-app
directive also tells AngularJS that the <div> element is the "owner" of the AngularJS application.Data Binding
{{ firstName }}
expression, in the example above, is an AngularJS data binding expression.{{ firstName }}
is bound with ng-model="firstName"
.Example
<div ng-app="" ng-init="quantity=1;price=5">
Quantity: <input type="number" ng-model="quantity">
Costs: <input type="number" ng-model="price">
Total in dollar: {{ quantity * price }}
</div>
ng-init
is not very common. You will learn how to initialize data in the chapter about controllers.Repeating HTML Elements
ng-repeat
directive repeats an HTML element:Example
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">
{{ x }}
</li>
</ul>
</div>
ng-repeat
directive actually clones HTML elements once for each item in a collection.ng-repeat
directive used on an array of objects:Example
<div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
<ul>
<li ng-repeat="x in names">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
Just imagine if these objects were records from a database.
The ng-app Directive
ng-app
directive defines the root element of an AngularJS application.ng-app
directive will auto-bootstrap (automatically initialize) the application when a web page is loaded.The ng-init Directive
ng-init
directive defines initial values for an AngularJS application.The ng-model Directive
ng-model
directive binds the value of HTML controls (input, select, textarea) to application data.ng-model
directive can also:- Provide type validation for application data (number, email, required).
- Provide status for application data (invalid, dirty, touched, error).
- Provide CSS classes for HTML elements.
- Bind HTML elements to HTML forms.
ng-model
directive in the next chapter.Create New Directives
.directive
function.w3TestDirective
, but when invoking it, you must use -
separated name, w3-test-directive
:Example
<body ng-app="myApp">
<w3-test-directive></w3-test-directive>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "<h1>Made by a directive!</h1>"
};
});</script>
</body>
Try it Yourself »- Element name
- Attribute
- Class
- Comment
<w3-test-directive></w3-test-directive>
Try it Yourself »
<div w3-test-directive></div>
Try it Yourself »
<div class="w3-test-directive"></div>
Try it Yourself »
<!-- directive: w3-test-directive -->
Try it Yourself »Restrictions
Example
restrict
property with the value "A"
, the directive can only be invoked by attributes:
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
restrict : "A",
template : "<h1>Made by a directive!</h1>"
};
});
Try it Yourself »E
for Element nameA
for AttributeC
for ClassM
for Comment
EA
, meaning that both Element names and attribute names can invoke the directive.
No comments:
Post a Comment