Style Directivies
Style Directivies
SYNTAX:
<element ng-style="expression"></element>
Angular JS Document [styleDemo.html]
<body ng-app="myApp" ng-controller="myCtrl">
<h1 ng-style="myObj">Welcome</h1>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.myObj = {
"color" : "black",
"background-color" : "khaki",
"font-size" : "60px",
"padding" : "50px”
}
});
</script>
</body>
Output:
The style parameters are set inside the controller
function.
Controllers:
• The use of controller in AngularJS is to control the
flow of data . The ng-controller directives is used
for this purpose.
• The controller is Javscript object containg
properties and functions.
• Each controller accept $scope as a parameter
which refers to the application that controller is
to control.
• We can use controller with properties, methods
and external files.
Example program,
<div ng-app="myApp" ng-controller="personCtrl">
FirstName: <input type="text"ng-model="firstName">
<br>
LastName: <input type="text" ng-model="lastName>
<br>
<br>
FullName: {{fullName()}}
</div>
<script>
var app=angular.module('myApp',[]);
app.controller('personCtrl', function($scope){
$scope.firstName = "John"; properties
$scope.lastName = "Doe";
$scope.fullName = function(){ Methods
return $scope.firstName + " " $scope.lastName;
};
});
</script>
output:
Script explanation:
In above script,
1.Two directives , div-app and div-controllers are
defined with attribute names ”MyApp” and
“MyController”.
2.The MyController function is a JavaScript function
that Angular JS invokes using the $scope object.
3.within the controller, two properties, ’FirstName’ and
’lastName’ are created and assigned values ‘John’ and
‘Doe’ using the $scope object.
4.The ng-model binds these input fields to the
controller properties.
5.The two properties firstName and lastName are
defined.Along with them , a method named fullName()
is invoked.
6.This function returns the string containing values to
the properties.
7.This function is then bounded to HTML.
Controller with external file:
Example for external files:
angular.module('myApp', []).controller('namesCtrl',
function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}
];
});